Reputation: 614
I have a simple inventory system with Canvas set to screen space - Camera. For some reason when setting icons to vector3.zero makes them go somewhere else.
first image is on start, everything works fine.
second image is apple being dragged, as you can see position is correct
once dropped as can be seen the apple goes to an unknown place.
This is the code for endDrag:
public void OnEndDrag(PointerEventData eventData)
{
if (item.category != ITEM_CATEGORY.Voxel)
{
icon.transform.position = Vector3.zero;
}
else
{
cube.transform.position = Vector3.zero;
}
}
nothing unique.
here is drag event:
public void OnDrag(PointerEventData eventData)
{
if (item.category != ITEM_CATEGORY.Voxel)
{
Vector3 screenPoint = Input.mousePosition;
screenPoint.z = 0.13f; //distance of the plane from the camera
icon.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
}
else
{
Vector3 screenPoint = Input.mousePosition;
screenPoint.z = 0.13f; //distance of the plane from the camera
cube.transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
}
}
Upvotes: 1
Views: 711
Reputation: 311
What you are seeing in the inspector of the RectTransform is the object's local position. But in the code you are manipulating the object's world position. When you set the object's world position to (0, 0, 0), it is unlikely that its local position will also be (0, 0, 0). What your code is doing is literally moving the object to the origin of the game world.
It's not Vector3.zero that is bugged (that doesn't make any sense), it's your code. In your OnEndDrag
function, try resetting the object's local position instead of its world position like this:
cube.transform.localPosition = Vector3.zero;
Idem for the icon of course.
Upvotes: 5