I'm trying to make a 2D shooter, the ship constantly looks at the mouse cursor and should fire with left click.
Right now I have
public GameObject bullet // The prefab
float nextFire;
void Update ()
{
// Movement Code
// Rotation Code
// Shooting
if (Input.GetMouseButton(0) && Time.time > nextFire)
{
nextFire = Time.time + 0.1f;
GameObject bulletInstance = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
bulletInstance.GetComponent().AddForce(transform.forward * 8000);
}
}
The ship is able to fire bullets, but they don't move. The prefab already has a rigidbody2D and the code compiles smoothly.
↧