Problem with GUI element following enemy

Hello guys I’m experiencing a little problem while setting a ui element to follow the enemies. Basically the UI moves with the enemy based on the position of the camera. If the enemy is in the middle of the screen the button is centered, but if the enemy is on the right the button is on the left and if it’s on the left, the button is on the right

enemy on the right of the camera:

2229982--148554--right.png

enemy on the left of the camera:

2229982--148555--left.png

and this is the code I’m using:

Vector3 screenPos = Camera.main.WorldToScreenPoint(unit.transform);
button.tranform.position = screenPos;

Bump

Don’t bother scripting this. Just make a world space canvas that is a child of the enemy.

1 Like

You should definitely do what BoredMormon says and just make it a child, but personally I think this offset-by-direction-and-distance effect is sort of sexy. Different strokes, I guess.

2 Likes

Definitely wouldn’t be the first time I’ve seen an unintended bug become understood and embraced as part of something new and amazing.

You could also do the code offset in world space, possibly making it simpler.

1 Like