Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

3D object rotating despite just changing its position

Discussion in 'Scripting' started by harry0647, Nov 8, 2020.

  1. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    Hello,

    I'm trying to move a 3D object by using keyboard control. However, despite just modifying the position of the object, the object itself also rotates toward the direction it is moving to:

    (I believe moving up or down is also the case, but isn't that clear compared to left/right)
    This is the simple code I have inside update():
    Code (CSharp):
    1.  
    2.         if (Input.GetKey(KeyCode.UpArrow))
    3.         {
    4.             transform.position += Vector3.up * Time.deltaTime * speed;
    5.         }
    6.  
    7.         if (Input.GetKey(KeyCode.DownArrow))
    8.         {
    9.             transform.position += Vector3.down * Time.deltaTime * speed;
    10.         }
    11.  
    12.         if (Input.GetKey(KeyCode.LeftArrow))
    13.         {
    14.             transform.position += Vector3.left * Time.deltaTime * speed;
    15.         }
    16.  
    17.         if (Input.GetKey(KeyCode.RightArrow))
    18.         {
    19.             transform.position += Vector3.right * Time.deltaTime * speed;
    20.         }
    and the only thing I have outside of that is defining speed. Could anyone tell me why does this code result in rotation of the object? Thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Do you have any other scripts on that object?
     
  3. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    I applied animator on it for another scene, but I don't think it works on this scene. Other than that, no I didn't.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can you show the inspector for the object?
     
  5. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    Ah, I believe it is shown in the streamable I upload?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I only see an animator no other script. Perhaps you have a different object selected?
     
  7. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    Oh, sorry I think what I showed is the prehab, the image below should be what you want.
     

    Attached Files:

    Last edited: Nov 10, 2020