Advertisement
LeeMace

Shooting

Aug 31st, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | Gaming | 0 0
  1. //in player behaviour
  2.  
  3. public GameObject bulletPrefab;
  4.  public float bulletSpeed = 100;
  5.  private bool isShooting;
  6.  
  7.  void Update() {
  8.  
  9.      isShooting |= Input.GetKeyDown(KeyCode.Space);
  10.  }
  11.  
  12.  private void FixedUpdate() {
  13.      
  14.      if (isShooting)
  15.      {
  16.          GameObject bullet = Instantiate(bulletPrefab, transform.position + transform.forward, transform.rotation);
  17.          Rigidbody bulletRB = bullet.GetComponent<Rigidbody>();
  18.          bulletRB.velocity = transform.forward * bulletSpeed;
  19.      }
  20.      isShooting = false;
  21.  }
  22.  
  23. //in bullet behaviour and attached to bullet prefab
  24. public class BulletBehaviour : MonoBehaviour
  25. {
  26.     public float onScreenTime = 3;
  27.     void Start() {
  28.  
  29.         Destroy(gameObject, onScreenTime);
  30.     }
  31.  
  32. }  
Tags: shooting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement