- Home /
Why is it moving locally instead of globally?
Sorry if if local vs global coordinates were extensevely explained before in this forum, but I still have doubst about it. I'm very noob at Unity, however usually I don't come with problems with local vs global positions. Recently I saw a tutorial and, from my understanding, it was explaining that "Vector3.up" is a global position, which supossedly means it will always points towards the global definition of "up", while a local position (example: transform.up) will change depending on where the object is facing. The problem is that right now I'm tryind to do a "snake-like" kind of game, but when I use "Transform.Translate(vector3.up)" it actually moves the snake always to where it is pointing to (it's a 2d game) depending of where it is facing, which is what I would expect of a local position system. If I use "transform.up", however, the snake always goes the same way no matter where it is facing (and it goes down instead of up), which in my understanding is supossed to be the behaviour of using local coordinates. What am I understanding wrong?
Note: the player starts rotated by -90 degrees in the z axis, not sure if/how this affects rotations
Answer by unity_ek98vnTRplGj8Q · Jul 30, 2020 at 10:44 PM
You are correct in your understanding of local vs global coordinates. If you look at the Unity Manual for Transform.Translate, however, you will see that it takes an optional second parameter that corresponds to the "space" in which you want to move. If you do not supply this parameter, it defaults to Space.Self. In other words, it will take any vector you give it and transform it into local space. So if you give it Vector3.Up, it will be transformed into transform.up. To fix this, you could tell it to move in world coordinates (or in other words, to not transform any vector you give it) by doing something like transform.Translate(Vector3.up, Space.World);
. Or you could take the more direct approach and modify the position directly like transform.position += Vector3.up;
Thanks you for your answer. Do you know why "transform.Translate(transform.up)" doesn't work?
Transform.Translate doesn't know anything about the vector you give it. If you give it transform.up it can only see the data that represents that vector, so it doesn't know that you are giving it a local vector. It will transform any vector you give it into local space, regardless of where that vector came from.
When I say that it is transfor$$anonymous$$g the vector into local space, all that means is that it is taking whatever vector you give it, then applying the rotation of your object to it. So if you start with Vector3.up it will rotate it by the objects rotation and you will get your objects up vector, which will equal transform.up. But since transform.up is just another vector, when you transform it into local space it will take that vector and rotate it again by the rotation of the object.
Vectors themselves are all inherently in global space. When you are talking about your "Local Up Vector" that simply means the vector in global space that corresponds to the up direction of your object.