Transform.forward problem (88045)

I am working on a tower defense game involving a tower shooting an enemy, but when the tower shoots, it shoots the bullet forward in the world direction instead of on its rotation. The bullet doesn’t go forward towards the enemy but goes forward in world space off screen.

I am using the following code to shoot the bullet from the tower script,

var ball = Instantiate(bullet, spawnPoint.transform.position, transform.localRotation);
ball.rigidbody.AddForce(ball.transform.localPosition.forward * 5000);

bullet is a GameoObject
spawnPoint is a Transform

Is there a new way to shoot something on its local forward axis? Was the .forward script changed recently, because most of the gun scripts are using it but it doesn’t work for me now. How do I fix the problem and shoot it forward.

transform.localPosition.forward ? This makes no sense, or will result in Vector3.forward. You should simply use transform.forward instead:

ball.rigidbody.AddForce(ball.transform.forward * 5000);

This line:

ball.rigidbody.AddForce(ball.transform.localPosition.forward * 5000);

ball.transform.localPosition represents the ball’s position, not the direction it’s facing, so I’m not sure what “forward” is relative to that. To be honest, I’m surprised that compiles.

You probably mean ball.transform.forward, which uses the ball’s rotation.