- Home /
How to get local coordinates of gameobject?
I need to get local coordinates of gameobject like on the screen. I can see it in Editor, but I need to get it in the code. I have tried to use Transform.localPosition, but transform.position = transform.localPosition.
Answer by ADiSiN · May 07, 2020 at 12:12 PM
Hi!
I am not sure what are you trying to achieve, but localPosition and position isn't equal, unless your GameObject don't have any parent.
Imagine you have GameObject attached to another GameObject.
Let's say parent GameObject has position of Vector3(0, 10, 0) and child it's own position of Vector3(0, -10, 0).
So if you will Debug.Log(transform.localPosition) the answer from Unity will be (0, -10, 0), because it's local position of child GameObject attached to parent GameObject. However, if you will Debug.Log(transform.position) the answer from Unity will be (0, 0, 0), because to calculate world position the engine will take into account parent position as well. So transform.localPosition is not equal transform.position, only in cases where your GameObject don't have any parent GameObject they will be equal, but you don't need localPosition in such cases.
If you want to find position of GameObject in 3D space projected onto screen space then you should use Camera.WorldToScreenPoint. You can find documentation about that function here: https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html
Your answer
Follow this Question
Related Questions
Unity3d and monodevelop c# scripting on ubuntu 16.04 IntelliSense problems 0 Answers
Vuforia Target Image scene not appearing in game 0 Answers
Making the player change his movement when it hit an object with collider 0 Answers
How can I Update c# version? 2 Answers
Making Everything Public 0 Answers