difference between transform.forward and vector3.forward
Hello, I'm learning unity C# and i'm confused between transform.forward and vector3.forward, please teach the difference between these two, here is the code i'm trying and they both giving different results(in this case i wanted vector3.forward)
transform.position = Pivot.position + transform.rotation Vector3.forward -Distance; transform.position = Pivot.position + transform.rotation transform.forward -Distance;
Answer by Hellium · Feb 11, 2017 at 08:32 AM
Vector3.forward
is the unit vector defined by (0, 0, 1)
transform.forward
is the forward direction of the object in the world space. It's the direction your object is "looking at", and depends on the various rotation you made on the object.
Supposing you instantiate a simple cube at the root of the scene, its forward
vector will be (0, 0, 1). Now, if you rotate the object 90° clockwise on the Y axis, its new forward vector will be (1, 0, 0)
But in your code, you have a specific case :
transform.rotation * Vector3.forward
makes the product of a rotation quaternion with the Vector.forward. This operation will result in a direction vector equals to transform.forward
Thus, I don't think transform.rotation * transform.forward
is what you really want to do.
Thanks for the great explanation.
So basically vector3.forward is the worlds forward direction(universal)
I still can't understand behind logic of transform.forward. Yes we use this to face character where it moves but I want to understand logic of this. For example how unity know where is the face of my character correctly ?
Every object in the game has its own local rotation, i.e the delta rotation in relation to its parent. The global/world rotation of the object is the composition of all those rotations.
Root → Local Rotation = Global Rotation = (0, 0, 0)
- Child → Local Rotation = (0, 90, 0) | Global Rotation = (0, 90, 0) ○ (0, 0, 0) = (0, 90, 0)
- Grandchild → Local Rotation = (90, 0, 0) | Global Rotation = (90, 0, 0) ○ (0, 90, 0) ○ (0, 0, 0) = (90, 90, 0)
The forward vector is the +Z axis obtained after calculating this global rotation.
Note that I've used the ○ symbol here to represent the composition for the sake of the example. Behind the hood its the product of quaternions, not of sum of euler angles. In the example I gave, it turns out to be the sum of the euler angles, but it's rarely the case
Answer by wrathofjuju · Nov 23, 2020 at 09:30 AM
This post is a couple of years old so not sure if anyone will see this, but I have a follow-up question (forgive me if this should go in a separate post):
I have a script attached to an object to make it rotate around its local Z axis like so: transform.Rotate(Vector3.forward, rotationAmount);
The funny thing is this is actually getting me the desired result, just not sure why:
The object this script is attached to, is the child of a parent object that is also being rotated.
Now, when I rotate the parent object, the child object's rotate script (using Vector3.forward) still seems to rotate the child object around its own LOCAL forward axis (which is actually the desired effect), whereas using transform.forward produces a strange result.
My question, I guess: what's causing this to be the case? Is this something to do with transformation order, or the child object inheriting the parent object's transform info?
I'm actually getting my desired result so I guess it's fine, just kinda baffled as to WHY :)
Yes, this question should have deserved its own post.
The behaviour you are seeing is normal. It's how the Rotate method works:
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
Use Transform.Rotate to rotate GameObjects in a variety of ways. [...] You can specify a rotation in world axes or local axes.
By default, it's the local axes which are used for the rotation. transform.Rotate(Vector3.forward, rotationAmount);
will rotate rotationAmount
around the vector Vector3.forward
of the local space of the object (`Space.Self`).
transform.Rotate(Vector3.forward, rotationAmount, Space.World);
will rotate the object around the world Vector3.forward
axis
Your answer
