- Home /
Question by
shantanumantriib · Nov 14, 2021 at 01:25 AM ·
uiunity 2d
Trouble with moving UI elements with Screen Space - Overlay setting
I would like the UI to show up next to the player sprite when I move it along a grid.
I've tried multiple solutions on the forum, but can't seem to get anything working. Here is my code:
print("Player Battle menu opened");
string playerId = clickedPlayerNode.getPlayerInfo().getPlayerId();
print(playerId);
Player playerClicked = GameObject.Find(playerId).GetComponent<Player>();
Vector3 playerSpriteVector = playerClicked.transform.position; //global position where I want the menu to appear
print(playerSpriteVector); //(-0.5, 1.5, 0)
RectTransform rt = playerBattleMenu.GetComponent<RectTransform>();
print(rt.transform.position);
Vector3 newPos = Camera.main.WorldToScreenPoint(playerSpriteVector);
print(newPos);
rt.anchoredPosition = newPos;
I tried changing the rt.transform.position too, but that doesn't seem to work either. The entire UI element just stays where it is originally. I added a picture for clarity. As you can see, it's nowhere near the sprite I moved.
Any help would be appreciated. Thanks!
capture.png
(14.8 kB)
Comment
Your answer
Follow this Question
Related Questions
Image as boundary 1 Answer
gameobject not setting active after timer ends 2 Answers
Change image in UI depending on the value of the slider 0 Answers
Screen scaling causing text to be outside button 0 Answers
Unity Android Screen Glitch 1 Answer