Transform.forward vs transform.position

what is the difference between doing transform.position=new Vector3(0,0,1) and using transform.forward?
yes i did looking in the scripting reference but i dug into the unity engine in order to find some code
and i found:

public Vector3 forward
        {
            get
            {
                return this.rotation * Vector3.forward;
            }
            set
            {
                this.rotation = Quaternion.LookRotation (value);
            }
        }

So i know vector3 forward is a property of Vector3 and transform which returns (0,0,1f) so what is the rotation part all about?

I too would like to here what others have to say on this

as far as I know from usage there pretty much identical

SIDE NOTE: @5vStudios i am not good at grammar myself in fact i suck but when it comes to spelling you said “there” instead of “they’re” you said “here” instead of “hear”.

I think there is something else to it.
So Vector3.forward is this in it’s simplest form:

public static Vector3 forward
{
    get
    {
        return new Vector3 (0f, 0f, 1f);
    }
}

transform.forward is this but with the rotation multiplied to it.

Transform.forward is the forward direction in local space. It takes into account the rotation in the hierarchy. It’s often used to move an object forwards, like this.

transform.position += transform.forward * speed * Time.deltaTime;

Vector3.forward is the forward direction in world space.

3 Likes

OO that makes more sense in other words if the game asteroids were to be recreated in unity3D you would use transform.forward!

That’s one example. There are a ton of others. Most movement systems use it. As do line of sight, facing, guns, and a whole host of other systems are often built with it.

transform has the variables forward, right and up. You can get the back, left and down vectors by doing their negatives. The Vector3 class has all of the six directions as variables - Vector3.right, left, up, down, forward and back are pretty usefull. You also have Vector3.one, which is equal to (1, 1, 1)

You can also set transform.forward to rotate your character. This means that you can rotate your character 90 degrees to the right by doing this:

transform.forward = transform.right

//or over time:
transform.forward = Vector3.Lerp(transform.forward, transform.right, Time.deltaTime);

I don’t see it done many places, but I don’t think it’s wrong in any way - quick testing has it looking pretty much identical to Quaternion.Lerp with similar values. This also means that you can replace the standard snippet to reset a transform’s rotation:

transform.rotation = Quaternion.identity

with this:

transform.forward = Vector3.forward

Not sure why you’d do that - the intention of the first form is more clear - but you can.

The 2nd form makes sense to me, it aligns the object.forward with the world.forward.

Why do that when you can use Vector3.forward?

Because when you use Vector3.forward in that example you would move in the direction of <0,0,1>

When you use transform.forward, you move in the direction that the GameObject is facing.

Vector3.forward is a global direction. It’s a direction that all GameObjects can agree on, despite their orientation or position in the hierarchy. It ALWAYS points in that direction. It’s a constant value.

Transform.forward as an individual GameObjects forward direction, relative to its orientation. If you turn the GameObject, then the Transform.forward returned is DIFFERENT from Vector3.forward.

That’s why the source code multiplies the rotation (orientation) by Vector3.forward. Of course that value is going to be different!

2 Likes

yeah i get that whole concept…

Then… why would you use Vector3.forward over Transform.forward to accomplish what was in BoredMormon’s example?

No it was in reply to munchy cause i did not know what he meant by The 2nd form makes sense to me, it aligns the object.forward with the world.forward.