Understanding transform.forward

Hi guys!

I have been looking for a couple of days for an answer for this question, I still do not know if its me doing something wrong or if its how unity is mean to work, I couldn’t find any answer to this questions so forgive me if it should be obvious or I’m looking for the wrong words, I think that the answer will probably be very simple.

I’m very confuse about the use of transform.forward in my game, basically because it does not work how I believe it should, first in one scenario when I instantiate an object I cannot use transfom.forward because it us null (it will be equal vector3.zero basically), but them after use transform.Translate the transform.forward get the right value and I can use it as normally, which end up forcing me to write this little piece of code:

    void MoveForward()
    {
        if (transform.forward.x == 0 && transform.forward.y == 0)
        {
        transform.Translate(PlayerRawDirection * movementSpeed * Time.deltaTime);
        }
        else
        {
            transform.Translate(transform.forward * movementSpeed * Time.deltaTime);
        }
    }

in this case when I first try to move this object forward is null so I need to get the “forward” position from somewhere else, after that, forward magically appear and I can use it normally, my questions is, why forward does not exist when I instantiate an object, why is it zero and why it magically appear when I use “translate”.

When something seems confusing like that, either use a step by step debugger or add logging (for exemple try Debug.Log(transform.name + " forward=" + transform.forward)) in relevant places for relevant data, should help you figure out what you’re doing wrong.

How is that?

I think those are rather different (if it were null accessing a non-static member would not give you 0 instead it would throw)

It’s a 3 dimensional vector I bet if you check .z that won’t be 0 in the above case. I’d think what you are seeing is just the default orientation

1 Like

that is correct, forward “1” is set in z, not sure why I was expecting it to follow my sprite direction or something, should I be aiming to use “up” instead when working in 2d?

Isnt “forward” your red arrow when you click an object in the scene? so Vector3.forward would just follow that?

Red arrow = X axis = left/right
Green arrow = Y axis = up/down
Blue arrow = Z axis = forward/backward
*up/down/left/right/fwd/bwd is a recommendation and not mandatory, feel free to treat any axis as any direction.

Depends if you’re viewing in global or local.

Vector3.forward is a shorthand for say Vector3(0,0,1), it points to global z+
transform.forward gives the what is the blue arrow when you have an object selected and viewing in local mode( i think it’s just ‘x’ to switch) it’s like saying transform.TransformDirection(Vector3.forward).
https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html

2 Likes

Oh right, I mostly work in 2d, I guess the vector2.forward is the x axis? Well that was good to know, thanks for clarifying, i don’t use them often, generally just create my own vector directions