Getting the reference to the rigidbody

As we already know, we need to get a reference to the rigidbody component so we can use it in the FixedUpdate() function to move the bullet. As such, let's declare another variable:

// [...] 
public class BulletController : MonoBehaviour { 
 
    public float speed = 10.0f; 
 
    public float timeBeforeDestruction = 10.0f; 
 
    private Rigidbody2D rigidBody; 
 
   // [...] 
} 

The Start() function is always a perfect place to initialize references to other components, so we can just write:

    void Start() { 
        rigidBody = GetComponent<Rigidbody2D>(); 
    } 

As a result, we are now able to use the rigidbody of the bullet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset