- Home /
How does TransformDirection(Vector) work?
I have this line of Code:
//dir variable is vector3 declare at the top
//input() is Vector3 function that return input from Key
dir = Camera.Main.TransformDirection(input());
rigidbody.AddForce((moveVector * 10f));
The function is transform direction from Local to World space and the Document is very scarce on what is it return. In this case, we use TransformDirection on Camera component and take input(), which is return World Space vector3, as argument.
What I want to know is does it take the input() and turn it into Camera.Main world direction or vice versa?
Local vs World space:
Local space exists within the objects localized confinement, being that if the object is parented to another object, then the child is now contained with in local space.
World space is the ACTUAL representation of the objects spaces in the entire 3D world, of course based on the originally deter$$anonymous$$ed 0,0,0 of starting space.
Now what TransformDirection does, is it takes a direction from LOCAL and converts it into WORLD, meaning that if an object is facing north, and it's parent is facing south, then the objects World direction is south. If you convert its local direction though, you will find it is actually facing north.
I hope this helped clarify.
Also, if you attempt to use TransformDirection on an object that has no parent, it will return the exact same as the direction that was input. So to clarify, It is best used like this.
Vector3 forward = transform.TransformDirection(transform.forward);
The documentation is actually quite clear on what this does. It just assumes you already read up on local and world space.
Does it mean
Camera.$$anonymous$$ain.TransformDirection(input());
transform.TransformDirection(input());
is indeed the same?
or the Camera.$$anonymous$$ain become the parent of the input() then it does the Local/World transformation?
Correct, it won't make any difference unless you are actually using the camera as your point, like this.
Vector3 dir = transform.TransformDirection(Camera.$$anonymous$$ain.transform.forward);
It is a function built into Transform, so any object with a Transform will have the TransformDirection option. Does not matter how you run it, the input/output is the relevant part.
Answer by Kishotta · May 06, 2017 at 05:49 PM
The idea behind TransformDirection is to take a a vector in local space and convert it to world space. Take some randomly rotated object. It's "forward" direction is ALWAYS <0, 0, 1> in local space. However, if it's rotated 45 degrees about the global Y axis, for example, that same forward vector might look like <0.52, 0, 0.52> in world space.
Your answer
Follow this Question
Related Questions
Rotate 3rd person camera around player 1 Answer
Advanced Camera Rotation 1 Answer
Rotate 3rd person camera around player 0 Answers