- Home /
Correct use of transform.position of UI element into game screen
Hello! I am trying to get my UI to transform into the position of my mouse cursor, when I click the mouse button. To give you an idea visually: I am making an action box for an adventure game in 2D Unity. So, whenever you rightclick (Mouse1) there will pop up an action menu at the right-clicked position in the game.
I have tried playing around with WorldToScreenPoint, but I get confused on how I really get this to work. Here is some of my code:
public class FrankMove : MonoBehaviour {
bool isMoving;
public bool showActionMenu;
GameObject actionMenu;
GameObject actionMenuBox;
private float Xdif;
private float Ydif;
public float speed;
private Vector2 positionHit;
private Vector2 positionDirection;
void Start() {
isMoving = false;
showActionMenu = false;
actionMenu = GameObject.Find("ActionMenu");
actionMenuBox = GameObject.Find ("ActionMenuBox");
actionMenu.SetActive(false);
actionMenuBox.SetActive(false);
}
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
//Movement
if(isMoving == true && showActionMenu == false) {
rigidbody2D.velocity = positionDirection.normalized;
if((positionDirection.x < 0.02f && positionDirection.x > -0.02f) &&
(positionDirection.y < 0.02f && positionDirection.y > -0.02f)) {
isMoving = false;
}
}
Xdif = positionHit.x - transform.position.x;
Ydif = positionHit.y - transform.position.y+0.3f;
positionDirection = new Vector2 (Xdif,Ydif);
if(Physics2D.Raycast(ray.origin,ray.direction)) {
if(Input.GetKeyDown(KeyCode.Mouse0)) {
positionHit = hit.point;
isMoving = true;
}
}
//ActionMenu scripting
if(showActionMenu == true) {
actionMenu.SetActive(true);
actionMenuBox.SetActive(true);
}else if(showActionMenu == false) {
actionMenu.SetActive(false);
}
/*********************HELP IS NEEDED HERE*******************/
if(Input.GetKeyDown(KeyCode.Mouse1)) {
showActionMenu = true;
//When ActionManu is true, I SetActive my UI gameobject.
//ActionMenu positioning over mouse should be here
}
}
}
If you want the menu to appear at the mouse position, you shouldn't even need WorldToScreenPoint — the mouse position is already in Screen coordinate space. Does that help?
Hmm. Well. this is what I'm trying to do, but it fails. It only moves the giant UI element in the inspector view a tad. Because the rest of my game is very small compared to the UI canvas in the inspector:
action$$anonymous$$enuBox.transform.position = positionHit;
$$anonymous$$y positionHit is where my mouse is clicked. This works fine on my walk-code. But, for the UI I need to scale it different somehow.
Answer by Glurth · Jan 07, 2015 at 04:10 PM
ActionMenu positioning over mouse should be here
ActionMenu.transform.position = Input.mousePosition;
(this will place the bottom right corner of the menu at the mouse position)
WARNING: This line at the beginning, will FAIL to find the actionmenu item, if it is deactivated when this call is made. Just discovered this annoying behavior myself while testing the above.
actionMenu = GameObject.Find("ActionMenu");
Awesome. This worked. Can't believe I failed to make the same connection from my previous code. But, I am still quite bad, so that's why I'm so confused at times, haha!
I am perfectly able to find the action$$anonymous$$enu item. It's all about when and how you setActives. I've done similar things in a past project without any failure yet.