I have a simple “shooter” component attached to my main character; let’s call him Cube Boy. Cube Boy fires a tiny cube whenever you press space. However, when you move Cube Boy while the bullet is still visible, the bullet moves relative to Cube Boy - not independently as you would expect. What’s going on?
Here’s the script attached to Cube Boy:
public class CubeShooter : MonoBehaviour {
public Rigidbody bullet;
public float speed = 10.0f;
void Start () {}
void Update () {
if (Input.GetButtonDown("Fire1")) {
FireBullet();
}
}
void FireBullet() {
Rigidbody bulletClone = (Rigidbody) Instantiate(bullet, transform.position, transform.rotation);
bulletClone.velocity = transform.forward * speed;
}
}