Untitled
public class PlayerShooting : MonoBehaviour { public GameObject bulletPrefab; public Transform bulletSpawn; public float bulletSpeed = 20f; void Update() { if (Input.GetButtonDown("Fire1")) { Shoot(); } } void Shoot() { GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation); Rigidbody rb = bullet.GetComponent<Rigidbody>(); rb.velocity = bulletSpawn.forward * bulletSpeed; Destroy(bullet, 2f); // Destroy the bullet after 2 seconds } }
Leave a Comment