Now that we have a prefab object to work with, we can instantiate and move copies of the prefab whenever we hit the left mouse button to create a shooting mechanic, as follows:
- Update PlayerBehavior with the following code:
public class PlayerBehavior : MonoBehaviour
{
public float moveSpeed = 10f;
public float rotateSpeed = 75f;
public float jumpVelocity = 5f;
public float distanceToGround = 0.1f;
public LayerMask groundLayer;
// 1
public GameObject bullet;
public float bulletSpeed = 100f;
private float _vInput;
private float _hInput;
private Rigidbody _rb;
private CapsuleCollider _col;
void Start()
{
// ... No changes needed ...
}
void Update()
{
// ... No changes needed ...
}
void FixedUpdate()
{
// ... No other changes needed ...
// 2
if (Input.GetMouseButtonDown(0))
{
...