I’m sorry if this has been answered before somewhere, but I’ve searched for quite a while and I am unable find a solution.
I’m making a basic shooter mechanic in Unity2D where the player shoots a gun that can be aimed with the mouse.
I’m trying to propel the bullet in the direction of the gun, but when it instantiates it just falls as if transform.forward wasn’t applied at all.
Code:
public Rigidbody2D bulletPrefab;
public Transform gunEnd;
if (Input.GetKey (KeyCode.Mouse0)) {
Rigidbody2D bulletInstance;
bulletInstance = Instantiate(bulletPrefab, gunEnd.position, gunEnd.rotation) as
Rigidbody2D;
bulletInstance.AddForce (gunEnd.forward * 5000);
}
However, by simply using transform.up instead, it works. (The following code is the same as above except its up, not forward):
public Rigidbody2D bulletPrefab;
public Transform gunEnd;
if (Input.GetKey (KeyCode.Mouse0)) {
Rigidbody2D bulletInstance;
bulletInstance = Instantiate(bulletPrefab, gunEnd.position, gunEnd.rotation) as
Rigidbody2D;
bulletInstance.AddForce (gunEnd.up * 5000);
}
I ended up rotating the bullet prefab and the transform point where its instantiated by 90 to make the illusion of transform.forward, but I still wanna know why transform.foward isn’t working.
Thanks in advance.