Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question The Cube moves upwards instead of moving horizontally

Discussion in 'Input System' started by Rajdeep100, May 4, 2021.

  1. Rajdeep100

    Rajdeep100

    Joined:
    Sep 11, 2018
    Posts:
    9
  2. Rajdeep100

    Rajdeep100

    Joined:
    Sep 11, 2018
    Posts:
    9
    This Code worked for me -

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         Vector3 movementInput = playerActionControls.Ground.Movement.ReadValue<Vector2>();
    4.         Vector3 move = new Vector3(movementInput.x, 0f, movementInput.y);
    5.         transform.position += move * speed * Time.deltaTime;
    6.  
    7.        // rb.velocity = movementInput * speed * Time.deltaTime;
    8.     }
    But how did the original code not worked for me.....
     
  3. 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);
     
    Rajdeep100 likes this.