Search Unity

Please explain me what I am missing

Discussion in '2D' started by bgmulti15a, Oct 15, 2017.

  1. bgmulti15a

    bgmulti15a

    Joined:
    Feb 9, 2017
    Posts:
    25
    Hi guys, today I feel really stupid and I'll explain you why.

    Today I had this problem: I wanted to move up my player along his green axis at constant speed.

    So I wrote a script and attached it to my player. The only line of code is this (inside of Update):

    transform.Translate(transform.up * Time.deltaTime * speed);

    So here is the problem: when the player has a rotation of 0 degrees everything is ok, the player moves along his green axis, it moves up.




    But then I try to rotate the player (45 degrees on his z axis) and it doesn't translate on his green axis anymore.
    Instead it moves horizontally on the world x axis.




    After some tries I've fixed the problem changing my code and adding this parameter:

    transform.Translate(transform.up * jump_magnitude, relativeTo: Space.World);

    Now the player always moves along his green axis independently by his rotation.

    Can someone please explain me why this work?

    Thanks you a lot!
     
    Last edited: Oct 15, 2017
  2. MiladZarrin1

    MiladZarrin1

    Joined:
    Jul 7, 2013
    Posts:
    78
    Translate() function translates the move with the object direction, into the world space. In other worlds when you use Translate() function, and say (for instance) Up, it thinks you mean its own up. If it is rotated, then it means something different that the actual up direction. As you can see when you rotate it, the green axis shows another direction and that's what the game object considers as up.

    I hope it is clear for you.
     
    bgmulti15a likes this.
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    bgmulti15a actually wants to make the gameObject go in the direction shown, in the direction the gameObject considers as up.
    But as the picture shows, the GO is going west instead of going north-west. I remember having such a problem some times ago, but can't remember why it happened.
     
    bgmulti15a likes this.
  4. bgmulti15a

    bgmulti15a

    Joined:
    Feb 9, 2017
    Posts:
    25
    Exactly, I don't understand why I have to set relativeTo variable to Space.World to make the object move along his "up" axis.
     
  5. MiladZarrin1

    MiladZarrin1

    Joined:
    Jul 7, 2013
    Posts:
    78
    Yes, You're right. Sorry. My bad :oops:
    I would suggest to test it with another version of unity. Maybe it was a system bug. I never experienced something like this.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Code (CSharp):
    1. // passing a world-space vector, used as local vector
    2. transform.Translate(Vector3.up * jump_magnitude, relativeTo: Space.Self)
    3.  
    4. // passing a world-space vector, used as world vector
    5. transform.Translate(transform.up * jump_magnitude, relativeTo: Space.World)
    So in the first line, Vector3.up is the world's Y axis. Using Space.Self takes that vector and converts it to the transform's local space, which changes it into the object's Y axis. With no transform rotation, the vector would stay the same.

    The second line already starts with the object's Y axis in world-space (transform.up), so Self.World does nothing except keep the function from automatically using Space.Self.

    relativeTo is an optional parameter that defaults to Space.Self, which is why your very first try didn't work. If you used Vector3.up instead of transform.up it would work.

    You saw your object moving horizontally because you're taking transform.up, which is a vector at a 45 degree angle away from the Y axis in world space, converting it into local space, which makes it a 45 degree angle away from the object's Y axis. Since your object is already rotated 45 degrees, and the vector is angled 45 degrees, you end up with a vector pointing 90 degrees away from the world Y axis, creating horizontal movement direction.

    https://docs.unity3d.com/ScriptReference/Transform.Translate.html

    There's always:
    Code (CSharp):
    1. transform.position += transform.up * jump_magnitude;
     
    Last edited: Oct 18, 2017
    bgmulti15a likes this.
  7. bgmulti15a

    bgmulti15a

    Joined:
    Feb 9, 2017
    Posts:
    25
    Awesome man, awesome.