0
\$\begingroup\$

I would like to show a menu to the right side and at the same y coordinate as a certain gameobject (shown as a green cube in the screenshot).

Therefore I'm trying to get the screen coordinates of the object in my scene using WorldToScreenPoint.

The code I'm using is this:

Vector3 nPos = new Vector3(GlowCube.position.x + GlowCube.localScale.x, GlowCube.position.y, GlowCube.position.z);
Vector3 nOff = Camera.main.WorldToScreenPoint(nPos);

The resulting values however don't look right to me. I have created a red rectangle in order to demonstrate where the coordinates "nOff" would actually be in the screen:

enter image description here

At first I thought I get these strange results because the GlowCube is parented to the case, but since I use GlowCube's world coordinates (GlowCube.postion...) and not its local coordinates (GlowCube.localPosition...), I thought that this wouldn't matter.

What am I forgetting here?

Thank you!

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I forgot to take into account that the object's origin is in its center and not at top / left.

The correct formula (if top / left is required) would therefore be:

Vector3 nPos = new Vector3(GlowCube.position.x + GlowCube.localScale.x - (GlowCube.localScale.x / 2), GlowCube.position.y + (GlowCube.localScale.y / 2), GlowCube.position.z); 
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .