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

[Repost] 3D object rotating despite just changing its position

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

  1. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    Hello,

    Sorry, this is a repost as my previous post here isn't getting me an answer.

    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 going up or down is also the case, but less clear)
    The following code is what I have inside update() of my objMover script, and the only other thing I have outside of update() is defining speed.

    Code (CSharp):
    1.  
    2.         if (Input.GetKey(KeyCode.UpArrow))
    3.         {
    4.             transform.position += Vector3.up * Time.deltaTime * speed;
    5.         }
    6.         if (Input.GetKey(KeyCode.DownArrow))
    7.         {
    8.             transform.position += Vector3.down * Time.deltaTime * speed;
    9.         }
    10.         if (Input.GetKey(KeyCode.LeftArrow))
    11.         {
    12.             transform.position += Vector3.left * Time.deltaTime * speed;
    13.         }
    14.         if (Input.GetKey(KeyCode.RightArrow))
    15.         {
    16.             transform.position += Vector3.right * Time.deltaTime * speed;
    17.         }
    Could anyone tell me why does this code result in rotation of the object? Thanks in advance.
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    I feel like I'm getting punked because.... there's no rotation whatsoever in the linked video? What are you talking about?

    edit: Are you talking about the slight perspective shift? If that's what you're talking about then maybe you want to change camera from Perspective to Orthographic?
     
  3. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    You're absolutely correct! I have no idea this is due to camera not the object itself...
    Could you explain how you know it's not rotation of the object? Is that because transform.rotation isn't changed at all? Thank you!
     
  4. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    That's the first clue, but I didn't even see it as rotation (as shown by the fact that at first I though there was no rotation at all) - I had to actually think harder to see something that might be misinterpreted as rotation.
     
  5. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    How would you define a rotation then? I thought the object having different sides facing the screen is called a rotation. Or am I missing something?
     
  6. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Not when perspective is involved. When a train runs past you from left to right, you see different sides of it as it moves, but it definitely hasn't rotated. Generally "rotation" means that it's rotated relative to the world.
     
    Bunny83 and harry0647 like this.
  7. harry0647

    harry0647

    Joined:
    Sep 8, 2020
    Posts:
    10
    I see. Guess what I'm missing is some readings regarding camera in Unity. Thank you so much for the replies!