Firing cannon -- how do I convert gun's eulerAngles to direction vector?

I have a cannon which has eulerAngle rotation of (x,y,z);

Is there an easy way to convert this into a directional vector so I can give a bullet
a velocity in the direction the gun is pointing?

Thanks.

Dan

use transform.forward (where transform is your cannon transform). If your cannon mesh is not aligned with any of the axes, (forward, up, right),
then make an empty game object, align its forward vector with the cannon mesh and make the cannon a child of this empty game object. Then do whatever operations you do (translate, rotate) to the empty game object and use transform.forward on the empty game object.

The gun will have a Transform element, whose right, up, and forward attributes point in those directions. Those are Vector3 objects, so I bet you can make use of them.

Edit: He beat me to it. :stuck_out_tongue:

Yep, drop the thought of having a rotation, make the physical cannon, rotate it to the desired position, then get the TransformDirection of the canon.

barrel.transform.TransformDirection(Vector3.forward);

isn’t that effectively the same as transform.forward, in many more characters?

It sure is… :smile:

cannongun.transform.forward seemed to work all by itself.
using TransformDirection didn’t make a difference

The only weirdness is that forward is actually backwards for the cannongun direction.
Maybe it has something to do with the original orientation?

Thanks everyone,

Dan

Did you make the model in a 3d application and import it into Unity? I’ve encountered some quirkiness with the z-axis being backwards when imported, that you either need to parent it to an empty gameobject and reverse it, or reverse it before you export it from your 3d app.

That’s because cannongun.transform.forward == cannongun.transform.TransformDirection(Vector3.forward).

You’re right, it has to do with the way the forward vector points for your model. If it’s backwards, either change the vectors within the Unity editor, or just negate the vector.