The Cube moves upwards instead of moving horizontally

I am using Unity’s new Input system and Unity 2049.4.25f1 Personal.

I was following samyam’s tutorial on YouTube,
I cross checked everything and everything seems normal to me, but the player gameobject does not function as intended.

PlayerController.cs

I’ve uploaded entire project on google drive - https://drive.google.com/file/d/1yIe6SIOAxU0tw1lbe-2vnoUH1yZlegwd/view?usp=sharing

I am new to unity and had read the docs about the new input system.
Thanks for your time!!

This Code worked for me -

private void Update()
    {
        Vector3 movementInput = playerActionControls.Ground.Movement.ReadValue<Vector2>();
        Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
        transform.position += move * speed * Time.deltaTime;

       // rb.velocity = movementInput * speed * Time.deltaTime;
    }

But how did the original code not worked for me…

If this is the original: // rb.velocity = movementInput * speed * Time.deltaTime;

Because you were transformed a Vector2 into a Vector3. The implicit conversion of Vector2 takes the existing x and y components and add a zero z component to it and that’s how you get the implicit Vector3. Which means your y change in your movement will end up in y in your Vector3 movement vector, so your object will move on the y axis which is the up-down axis in Unity. When you changed your code, you moved over the Vector2.y into Vector3.z which is one of horizontal axes in Unity so your movement will be on the horizontal plain.

Original code:
Vector2(10, 20) -> Vector3(10, 20, 0);
This version:
Vector2(10, 20) -> Vector3(10, 0, 20);

1 Like