Why should I use TransformDirection function for firing projectiles?

I’m playing around with a tutorial on firing a projectile and I came across a bewildering issue. It would seem that my understanding of Local and World Space is opposite to what they truly are.

This is what I know about Local and World Spaces.

World Space is referenced from the World Origin (0,0,0), i.e Static and always the same direction

Local Space is referenced from the Game Objects Origin(Center of the Object). i.e Dynamic and changes orientation as the Object is rotated.

From this understanding it would seem to me that World Space Axis’s are the same for all objects in the Scene regardless of their rotations or positions since it is referenced from the world origin and not the individual object’s origin. So regardless of whether i rotate my character 90 degrees or 120 degrees, The projectiles should still fire in the same direction using World Space, in this case the World’s z-axis.

It would seem that if I wanted my projectiles to fire along my characters local z-axis’s I would want to use Local Space Coordinates. That way my projectiles fire from the front of my character at all times, even if it is rotated in different directions.

This is the line of code this is bothering me.

clone.rigidbody.velocity = transform.TransformDirection(Vector3

the clone variable is a GameObject that I instantiate as my projectile when i click the Fire1 button. Now the tutorial says to use the TransformDirection function to convert from Local Space to World Space.

when i run the script using Transform.Direction, i.e. using World Space, it works perfectly. when i click Fire1, it fires from the front of my character no matter where it is looking (Which I thought was what Local Space was for). When i remove the TransformDirection function from the line of code, i.e using Local Space, my character fires in the same direction regardless of where it is aiming (Which I thought was what World Space was for).

So the results of running my script are the exact opposite of what I thought i knew about Local vs World Space. I could just accept what the tutorial says to do but I would rather rectify my misunderstanding on this concept for future projects. If anyone can help clarify this for me I would greatly appreciate it.

BTW I’m an engineer and quite familiar with the concept of vectors and coordinate planes etc… Or at least i thought i was =P I must be either making a stupid mistake with this or perhaps its a project setting in Unity?

In fact you don’t need TransformDirection for this, just use

clone.rigidbody.velocity = transform.forward * speed;

Anyway, rigidbody.velocity is world space, so you need to convert local space to world space in order to get the velocity to go in the right direction.

Local Space is a shortcut – really nice to say things like “3 meters to my left”. But, Unity can only do things “for real” in standard (x,y,z) coords, which is World Space.

A tricky part is, does 5 mean five feet or 5 yards? Neither/both. You have to say. Likewise (2,0,5) is local or world depending how you think of it.

If you’re “thinking” feet when you say 5, you have to convert to 1-2/3s yards when ordering carpet. Likewise, if you’re thinking “2 to my right and 5 ahead” for (2,0,5) then you’re using Local, so have to ask Unity to please compute the actual (x,y,z) world numbers where that is. Put another way, if in your mind (2,0,0) means East, then you’re done (since you were thinking World.) If you were thinking My-Right, then you have to figure out the real NESW to go there.

A last trick is that transform.forward is in world coords??? That seems to make no sense, since “1 ahead of me” is clearly local. What it really means is to first think “1 ahead of me,” step there and then convert that to the real (x,y,z) so you can use it(*).

In TransformDirection, think of Transform as a noun, not a verb. fred.TransformDirection(0,0,5) means to go +5 in some Z. Going 5 North would be pointless (I can compute Z+5 myself.) Ah … it means to go 5 in the facing direction of the Transform, fred. If we ask where that is and were told “it’s 5 ahead of fred” that would just be pointless. Clearly, the answer should be the standard (x,y,z) where that puts you.

(*) transform.forward isn’t really 1 ahead of you. It’s the real, small (x,y,z) to move from you to 1 ahead of you.