Camera transform forward doesn't seem to be really forward

Hey. I’ve been watching a tutorial on how to setup a character walking in 3d isometric view.

This is the basic script:

public class CharController : MonoBehaviour
{
    [SerializeField]
    float moveSpeed = 4f;

    Vector3 forward, right;
    // Start is called before the first frame update
    void Start()
    {
        forward = Camera.main.transform.forward;
        forward.y = 0;
        forward = Vector3.Normalize(forward);
        right = Quaternion.Euler(new Vector3 (0, 90, 0)) * forward;
      
      
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.anyKey){
            Move();
        }
    }
    void Move()
        {
            Vector3 direction = new Vector3(Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
            Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
            Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");

            Vector3 heading = Vector3.Normalize(rightMovement + upMovement);

            transform.forward = heading;
            transform.position += rightMovement;
            transform.position += upMovement;
        }
}

Now, as you can see, the player is properly walking: Screen capture - 5d12a782c3e0ab232576c947327aadbe - Gyazo

I’ve been going through this entire code, and what I don’t understand is this:
forward = Camera.main.transform.forward; (located in void Start())

The camera is a child of an object that is rotated 30 degrees toward the ground, like this:

The character’s supposedly forward vector is the camera’s forward vector, right?
But if it was really the case, the character was to be facing 30 degrees into the ground or to the air, depending on the direction.

I would be happy if someone could explain how the code is properly working, even though the camera’s forward is “wrong”.

I hope it’s clear.

Have you missed line 11 in your code? That says:

forward.y = 0;

after that the vector is only in the x-z plane since you just set the y component to 0. Now the vector is no longer normalized. That’s why the next line is

forward = Vector3.Normalize(forward);
1 Like

After reading this answer again, I have to admit that something is still not sitting right with me.

the rotation of 30 degrees toward the ground is on the x axis, therefore resetting the y axis to 0 should have no effect of this kind, but rather make it even worse?

You confuse euler angles (which is a set of 3 angles which represent 3 consecutive rotations) with a direction vector in space.

If you have trouble understanding the concept of vectors and coordinate spaces, I would recommend to watch the 3b1b series on linear algebra.

The rotation of an object rotates the whole object and its corresponding local space. Transform.forward returns the worldspace forward direction of the object in question, in your case your camera. As you said, the vector is pointing downward initially. Down in worldspace is the -y direction. When you set the y component of the vector to 0, the vector now lives in the x-z plane, the horizontal plane.

I don’t know how we should explain the concept of a vector any simpler ^^.

Thank you, I think I get it now.