R
R
Relayz2020-10-29 22:02:38
Unity
Relayz, 2020-10-29 22:02:38

How to constrain the movement of a 2D object in Unity3D?

Hello! There is a panel on which pictures of balls are located. Implemented movement trace. way:

public void OnDrag(PointerEventData eventData)
    {
        dot.transform.position = eventData.pointerCurrentRaycast.screenPosition;
        //dot.transform.position = new Vector3(
        //    Mathf.Clamp(eventData.pointerCurrentRaycast.screenPosition.x, minPosX, maxPosX),
        //    Mathf.Clamp(eventData.pointerCurrentRaycast.screenPosition.y, minPosY, maxPosY),
        //    0);
    }


The second option with the clamp does not work, for some reason the balls fly sharply into the corner. Although I entered adequate coordinates for the restrictions, they fly away for them and cannot return. In general, I do not know what to do here?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question