Shoot forward diagonally

This is what I have to make a guy shoot forward at a speed of 1600. When he is facing right he shoots right and when he is facing left he shoots left:

bullet.rigidbody.AddForce(transform.forward * 1600);

How do I make him shoot diagonally up the way he is facing? So, shoot upwards right and upwards left depending on whether he is facing right or left?

This applies an equal amount of force forward and upward:

bullet.rigidbody.AddForce (0,1600,1600);

That would give the bullet too much speed (the speed would be 1600*sqrt(2) instead of just 1600)…

What you would do would be this, which would ensure he could fire in whatever direction he wanted:

bullet.rigidbody.AddForce(transform.TransformDirection(Vector3.forward) * 1600);

Yes, and for my own understanding, Vector3.forward alone will not achieve diagonal movement, is this right? Either the transform in this case is forward facing the bullet’s target, or Vector3.forward should be replaced with a diagonal vector.

But don’t you want to shoot in whatever direction your avatar is facing? The code above would ensure that, as his local forward vector would be made into a world vector by the TransformDirection function. The resulting vector of transform.TransformDirection(Vector3.forward) is diagonal, if your avatar is facing diagonally.

If you want him to shoot diagonally, even if he’s facing up, that’s a different scenario than what I expected.

I believe the scenario is that the player faces left and right, so only shoots left and right. In order for transform.TransformDirection(Vector3.forward) or even transform.forward to work diagonally, a script to aim diagonally would have to be written.

You may be right, I have no idea anymore :slight_smile: It doesn’t change the fact, to shoot diagonally by some force, you can’t put that force on two components of the vector. That will make the vector.magnitude be sqrt(2) too long.