Can someone explain the vector math please

Hi

I’m referring to the vector math used to calculate m_CamForward where it scales and then normalizes in the PlayerMovement script.

var camTransform = Camera.main.transform;
var m_CamForward = Vector3.Scale(camTransform.forward, new Vector3(1, 0, 1)).normalized;

I noticed if I remove the normalize part that he just walks along the vertical axis but with it normalized he then runs along the vertical axis. I don’t understand what’s happening behind the curtain with the scaling and then normalizing.

Thanks.

1 Like

I think I’ve found out what is happening here. If I’m wrong, please correct me!

        // Read inputs.
        // These are defined under Project Settings > Input.
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        // Calculate camera relative direction to move.
        // Scale multiplies two vectors component-wise.
        // Take m_Cam.forward (the blue Z axis of the camera), X and Z stay same, Y (height) is ignored. 
        // Set vector length to 1 with .normalized (we only need direction, not length). 
        // It makes no difference, but is just a convention showing that we're not going to do anything with the length.
        // Ignoring Y is just for clarity purposes. 
        // It makes no difference, but shows that we're not going to do anything with the y value.
        // Basically we project the blue axis of the camera on the ground this way.
        // Take m_Cam.right (the red X of the camera). 
        // This movement direction has no height as it merely indicates the cameras rotation towards the XZ plane (the ground) and not the tilt.
        Vector3 m_CamForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
        // Take the input "strength" and apply that to the calculated the move direction. 
        // (The further you move, the farther you go).
        Vector3 m_Move = v * m_CamForward + h * Camera.main.transform.right;
        m_Character.Move(m_Move, false, false);

Late to the party, but I helped someone else with this and thought I would share.

First, the .normalized just normalizes the MAGNITUDE of the vector. magnitude = LENGTH of a vector. What does this mean? If we normalize the vector, we are setting the length of it to 1, while keeping the same DIRECTION of the vector. Thats really it. But now the why.

If you do not normalize it…and your vector happens to have a length of less then 1, then you will be multiplying v in the v * m_camForward part with a number less than 1…which will result in a slower move speed. We are adjusting most of our characters move speed based on the speed variables, so we always want to multiply our controller joystick input, v, by a vector with a length of 1. The most important part of m_CamForward is the DIRECTION if gives us, it’s length always needs to be 1. This is really all .normalized does. It changes the vector around so that it’s length is 1, but it points the same way. lets do this in 2D. Imagine a line on a graph coming from (0,0) and ending at (0.5, 0.5). This line has a direction, and it has a length(around 0.707ish). If we wanted to use this in our code, and we had the joystick full forward (which would make v = 1) we would be multiplying 1*0.707 and get 0.707 as a direction. Now, this might seem like an issue…but imagine we are moving up and SLIGHTLY to the right… what does this look like? Well, V = 1 still (full forward on the joystick) and H would equal lets say .2. Why only .2? Because we are barely pushing right on the joystick. Full right on the joystick = 1. So now you can see that the lower the number the less movement in each axis we expect to move. So the Move code will read the 0.707 as “we are only pushing 3/4 of the way up one the joystick”. Now depending on how much you have moved/rotated your camera, the actual numbers that the move vector has might not make sense (if you rotated it 90 degrees, you would see 1 in the Z axis instead of the X for example) but this is just the code compensating for the camera angle. But the vector length of move still equals out to be 1, providing you are pushing fully in a single direction, because we first normalized the m_CamForward first.

3 Likes

That explanation actually really helps me visualize what is going on, I sort of had an idea but that really cleared it up for me in a nice way.
Thanks for taking the time to send that.:+1::+1:

2 Likes

Privacy & Terms