Question by
Johandea · Oct 20, 2018 at 12:47 PM ·
mousepositionlocalspace
Mouse.position to local space (UI)
Hey So I'm trying to make a UI element follow my cursor. Since the object is a child of a canvas I've found it is a lot easier to make them do what I want by using its local space, opposed to world space. But I've run into s problem when converting my mouse position into local space... From what I understand the code below should work, but as you can see in the gif it only results in a fraction of the correct movement.
if (followMouse)
{
Vector3 tempPos = transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
tempPos.z = transform.localPosition.z;
transform.localPosition = tempPos;
}
It feels like I've tried every possible combination of screen.to*somepoint* and transformPoint. The result is always ways of and I'm officially out of ideas. So how have you solved this? Is there a correct way of doing this?
tempgif.gif
(143.9 kB)
Comment