So I'm following a tutorial online and am 99.9% sure I have not made an error and it is perhaps something to do with my trying to implement his ideas in to an already established project with (perhaps) different concerning factors.
The general idea from the math is that my SlotScript
which implements IDragHandler
, OnDrag
-
public void OnDrag(PointerEventData data)
{
if (inventory.Items[slotNumber].itemName != null)
{
inventory.ShowDraggedItem(inventory.Items[slotNumber]);
}
}
executes a method in my inventory which simply assigns bool draggingItem
as true and changes it's sprite along with making it active.
Now my actual update code for returning the dragged item's position isn't returning it's expected value:
void Update()
{
if (draggingItem)
{
Vector3 position = (Input.mousePosition - (GameObject.FindObjectOfType<Canvas>().GetComponent<RectTransform>().localPosition));
draggedItem.GetComponent<RectTransform>().localPosition = new Vector3(position.x + 15, position.y - 15, position.z);
}
}
Here are some examples of what I get. When the mouse is in the center of the canvas I get the expected result:
If I drag the item outwards to any edge the value becomes distorted always returning less than the value should be, for example:
Other information:
Screen space is set to overlay.
Native resolution is at 1920x1080
.
When tested on a smaller resolution size the problem is worsened (the offset is proportionally larger).
Once the onDrag
event occurs the mousePosition
updates correctly and the Canvas
localPosition
stays exactly the same (expected behaviour?)
I hope I made this clear enough.