GeekCodingMaster GeekCodingMaster - 4 months ago 41
C# Question

Objects gets big when dragged around Unity C#

I have problem when I run this script on some gameobject and I drag it around it gets really big by somehow changing the z positon

public class Draggable : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
}


Before http://prnt.sc/c2e7t7

After http://prnt.sc/c2e7wb

Even if I attach it to brand new
Image
object for example it always resizes it to this exact Z value -708.4954

Update Added small sample project where the problem still occurs : http://s000.tinyupload.com/?file_id=65691163815194240868

Answer

That's likely because the Z axis is changing when you did transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);, You should store that to a temporary value then change z value to 0 before assigning it back to your Object's position.

public void OnDrag(PointerEventData eventData)
{
    Vector3 noZValue = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    noZValue.z = 0;
    transform.position = noZValue;
}

If this does not solve the problem then you are are modifying the Object scale or pos from another code and you should as well show that part of code. For your information, the Object is not actually scaling with that code. It is moving closer to the camera making it look big.

EDIT:

The code in your question is a way to move Sprites/Sprite Renderer not UI components such as Image, Button, RawImage....

This should move your UI when your UI Canvas is in Screen Space - Camera Mode:

public class Draggable : MonoBehaviour, IDragHandler
{
    public Canvas parentCanvas;
    public void OnDrag(PointerEventData eventData)
    {
        Vector2 movePos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, eventData.position, parentCanvas.worldCamera, out movePos);
        transform.position = parentCanvas.transform.TransformPoint(movePos);
    }
}

If you try this, you will realize that when you click on the image, the image will jump to the click position. You need to add offset pos when moving the image to fix this. Just get the mouse pos when the mouse button is down. Use that to add to the current mouse pos when dragging. This can be done by implementing IBeginDragHandler and using the OnBeginDrag function.

Complete UI Image move with offset:

public class Draggable : MonoBehaviour, IDragHandler, IBeginDragHandler
{
    public Canvas parentCanvas;
    Vector3 Offset = Vector3.zero;


    public void OnBeginDrag(PointerEventData eventData)
    {
        Vector2 pos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, eventData.position, parentCanvas.worldCamera, out pos);
        Offset = transform.position - parentCanvas.transform.TransformPoint(pos);
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 movePos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parentCanvas.transform as RectTransform, eventData.position, parentCanvas.worldCamera, out movePos);
        transform.position = parentCanvas.transform.TransformPoint(movePos) + Offset;
    }
}