- Home /
Confusing with transform.position and transform.localposition in the 2.5d view
Hi,
I have a scene that contains a main camera rotated with Z=27 and many ROADS, Above the ROAD we have an obstacle the view is like the image :

Obstacle Holder Script :
public class ObstacleHolder : MonoBehaviour
{
public Vector3 transformPosition;
void Update()
{
transform.position = transformPosition;
}
}
I noticed that when i change the transform.position.x the obstacle moves horizontal on the x axis like the green axis image and the x and y of the transform.localposition change to make the obstacle move inside the roads can any one explain me why transform.position move like the green image and localposition move like the red image ?

I would like to understand more what is transform.position and transform.localposition in this case.
I know that :
Transform.position is The position of the transform in world space.
Transform.localPosition is Position of the transform relative to the parent transform.
but i don't understand what does it mean in this example i am confusing with that
the project is here
Answer by ecv80 · Dec 11, 2018 at 03:39 AM
Hi,
Your obstacles are parented to your camera, and as such their local coordinates are relative to it.
Originally your camera X and Y axes were aligned to world axes. But now that your camera is rotated around Z axis in world coordinates, whatever children of your camera, "inherits" the camera space. That is to say, no matter your camera rotation, its children will always lean to the camera left side when you move them towards the left by offsetting their "local position". If you simply offset their "position" to the left, they would go left relative to world space instead. Same applies to rotations and scale.
EDIT: So I guess either unparent your obstacles from the camera or make sure you use the right coordinate space.
thank you so mush +vote i understand now the mechanism but i have one last question if we want to calculate manually transform.position of the obstacle 1. we have the camera position is (2.36 , 1, -10) and the 01 (child obstacle) position is (-2,0,0) . so i made print (Camera.main.transform.position+transform.localPosition );is that forumula correct ? i think is correct when we haven't rotation so in rotation how to calculate it ? I have calculate it using the math theory : if we move the obstacle object with x position relative to the main camera x:-2.81 and rotation of the camera is 27 so the result is : transform.position.X=-2.81*$$anonymous$$athf.cos(27) transform.position.Y=-2.81*$$anonymous$$athf.sin(27) the result is (-2.5,-1.27) the same as transform.position
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
KEEP INSTANTIATE PREFAB ONMOUSEDOWN 1 Answer