Linking your Achievements/Stats to your in-game functionality
Overview
Example
public class GunFire : MonoBehaviour
{
public GameObject bulletPrefab; // Assign your bullet prefab in the Inspector
public Transform bulletSpawn; // Assign the spawn point for the bullet (usually at the gun's barrel end)
public float bulletSpeed = 1000f; // Speed at which the bullet will be fired
void Update()
{
if (Input.GetButtonDown("Fire1")) // Default fire button (usually left mouse button)
{
Fire();
}
}
void Fire()
{
// Create a new bullet instance at the bulletSpawn position and rotation
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
// Add a forward force to the bullet to propel it forward
bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed);
// Optional: Destroy the bullet after a certain time to avoid memory leaks
Destroy(bullet, 2.0f); // Destroys the bullet after 2 seconds
}
}Last updated