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
(0,0,speed));
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?