0
\$\begingroup\$

This will be hard to explain, but I'll try to keep it short and simple:

I'm trying to set the position of a UI rectangle to the position of the mouse when clicked. When initialized, the position of the rectangle works fine (if, during Awake(), I initially set it so that x = 100 and y = 100 and then check the Rect Transform component during run time, Pos X and Pos Y are both set to 100 as intended). However, if I use the X and Y coordinates of Input.mousePosition, Pos X and Pos Y are given strange numbers that I have no clue where they're coming from.

I don't think they're pixel coordinates because the value for Pos X of the rectangle when my mouse clicks at the farthest left possible during run time is a value over 300 when it should be 0, and that value goes up as I progressively click rightwards.

Now here's the strange bit. When I try to use print() to print the mouse position x and y values that I use to set the rectangle's position, they're printed as intended (clicking the bottom left of the game area yields x = 0 and y = 0) yet the rectangle's position is far from close the values printed (rectangle's position is x = 337.5 and y = 192.5).

Can someone please explain why this happens, and how I can fix it? I suppose I can manually fix it by subtracting the new position values by 337.5 for x and 162.5, but it feels too brute-force-ish and there's bound to be consequences down the line.

Here is a screenshot to help clarify my issue: https://i.stack.imgur.com/6xebh.png Mouse Input Issue

\$\endgroup\$
1
  • \$\begingroup\$ if your rectangle is a child of any gameobject then try to set position instead of localPosition \$\endgroup\$ Dec 31, 2015 at 10:06

2 Answers 2

2
\$\begingroup\$

localPosition and position has no difference if your GameObject is not a child of any other Gameobject. But if your GameObject is a child of any other GameObject then localPosition represents the position with respect to its parent. For example, if your parent has position 10,10,10 and its child localPosition is 0,0,0 then your child object is actually at 10,10,10(Globally). If you change your GameObject's localPosition to 10,0,0 then it will replace its position and add 10 units in X-axis of parent. Now the localPosition of child is 10,0,0 and position (aka Global Position) will become 20,10,10.

In your case rectangle is a child and you are setting localPosition and giving it coordinates according to Global Position. That's the point.

I hope you got my point.

\$\endgroup\$
2
\$\begingroup\$

If you're using localPosition, the position will be relative to the GameObjects parent. You should use position instead, if you want to work with absolute coordinates.

\$\endgroup\$
1
  • \$\begingroup\$ It works! But why does it use absolute coordinates when I used localPosition in line 16? \$\endgroup\$
    – OmarK
    Dec 31, 2015 at 10:13

You must log in to answer this question.

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