Moving the bullet

In a similar way to the player controller, we need to move the bullet. We use the same equation for the motion and the same lines of code. However, we need to make the movement happen only on the y-axis; there is no clamp, and the bullet moves regardless of the input of the player. Here is the final code, which shouldn't be hard for you to understand:

    void FixedUpdate() { 
        //Get the new position of our bullet 
        var y = transform.position.y + Time.deltaTime * speed; 
 
        //Set the position of our bullet through the RigidBody2D component (since we are using physics) 
        rigidBody.MovePosition(new Vector2(transform.position.x, y)); 
    } 
It's worth mentioning here that if we have the speed as a negative value, the bullet will move down rather than up. We will use this to make the enemies' bullets; their speed will be negative. 
..................Content has been hidden....................

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