Advertisement
Guest User

Bullet

a guest
Feb 28th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. public class Bullet : MonoBehaviour {
  2.  
  3.     public float speed;
  4.     public Rigidbody2D rb;
  5.     private Vector2 direction;
  6.  
  7.     void Start()
  8.     {
  9.         rb = GetComponent<Rigidbody2D>();
  10.     }
  11.  
  12.     private void FixedUpdate()
  13.     {
  14.         rb.velocity = direction * speed;
  15.     }
  16.  
  17.     private void OnBecameInvisible()
  18.     {
  19.         Destroy(gameObject);
  20.     }
  21.  
  22.     public void Initialize(Vector2 direction)
  23.     {
  24.         this.direction = direction;
  25.     }
  26.  
  27.     public void OnCollisionEnter2D(Collision2D collision)
  28.     {
  29.         if (collision.gameObject.tag.Equals("Enemy"))
  30.         {
  31.             Destroy(gameObject);
  32.             Debug.Log("Bullet has been destroyed");
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement