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”.
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
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 ^^.