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

Moving rotated object

Discussion in 'Getting Started' started by Goodeddie, Aug 4, 2016.

  1. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Hello.

    I have rotated an object so that it looks different but after rotating it, the axis changed and are not the same axis as the environment.

    How do I move the object in relation to the environment and not the axis it is using after rotating?
     
  2. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    I found out I can do something like transform.Translate(vector3.forward, space.world) to move in relation to the world.
     
    karaustabusraa and gortyn98 like this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Or you can do something like transform.position += vector3.forward.
     
  4. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    How would you get the vector between two vectors?
     
  5. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Well I can't do vector3.forward because my object was rotated.
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    One possible way would be to have an empty GameObject as the parent of your rotated object. Use that object for moving around, and the child for rotating.

    Or, more simply, you can use the relativeTo parameter of the Translate method and move it in world space.
    Code (CSharp):
    1. transform.Translate(Vector3.forward * Time.deltaTime, Space.World);
     
    DevMayCare and Tset_Tsyung like this.
  7. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    OH I see! That's smart! One object for movement and the other for rendering. Anyway, I ended up using a custom Vector3. If I wanted to move in the X axis AND Z axis, I used new Vector3(-1, 0, 1)
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes you can. Try it. (Hints: Vector3.forward is not the same thing as transform.forward, and adding to the .position is not the same thing as using .Translate in object space mode.)
     
    Tset_Tsyung likes this.
  9. Goodeddie

    Goodeddie

    Joined:
    Aug 3, 2016
    Posts:
    79
    Ok thanks. I think I don't have to worry about it anymore because I'm going to have an invisible capsule controlling the movement and then just render a sprite over it that is rotated.