2

I am implementing a drag and drop function for my Unity Game, and am having difficulty setting the boundaries of the drag and drop. This is my OnDrag code.

    private Canvas canvas;
    private Vector2 pos;
        public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag");

        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out pos);
        Vector3 draggablePos = draggableObject.transform.position;
        transform.position = canvas.transform.TransformPoint(pos);
       
        if (draggablePos.x <= -2)
        {
            print("outofbound");
            draggablePos.x = -2;
        }

        if (draggablePos.x >= 2)
        {
            print("outofbound");
            draggablePos.x = 2;
        }

        if (draggablePos.y <= -1)
        {
            draggablePos.y = -1;
            print("outofbound");
        }

        if (draggablePos.y >= 1)
        {
            draggablePos.y = 1;
            print("outofbound");
        }

    }

Getting the position of the draggable game object and detecting if it was out of bounds worked, as my console logged the debug message. However, the transform of the game object was not set to the correct position and apart from the debug message, there are no other visible effects of going out of bounds. I tried googling around and still it did not work. Here is what I did:

  1. From these 2 links, I thought that I might need a reference to the draggable game object to set its transform. Then I might also need a new Vector3 to set the position. Set value on GameObject.transform.position[index] with C# How to transform a gameObject to a transform variable

To get a more explicit reference to my draggable object, I added this code. The script is attached to the draggable object.

public GameObject draggableObject;

 private void Awake()
    {
        draggableObject = this.gameObject;
    }

To get a new Vector3 position, I tested it on Vector3.zero, but still it did not work... The debug message was logged but my game object's position was still on an out of bounds area. The code:

if (draggablePos.x <= -2)
        {
            print("outofbound");
            draggablePos = new Vector3(0, 0, 0);
        }

        if (draggablePos.x >= 2)
        {
            print("outofbound");
            draggablePos = new Vector3(0, 0, 0);
        }

        if (draggablePos.y <= -1)
        {
            draggablePos = new Vector3(0, 0, 0);
            print("outofbound");
        }

        if (draggablePos.y >= 1)
        {
            draggablePos = new Vector3(0, 0, 0);
            print("outofbound");
        }
  1. Since the coding was not working so far, I tried creating invisible box collider2D with sprite renderer disabled but that did not work too.

How can I set the boundaries for the draggable game object?

1 Answer 1

1

This is a script to bound an object in an area.

public Rect ScreenBounds;
public bool DrawBoundary;

void LateUpdate()
{
            
    clampToBoundary();
}

private void clampToBoundary()
{
    Rect boundary = ScreenBounds;
    
    transform.position = new Vector3(
        Mathf.Clamp(transform.position.x, boundary.xMin , boundary.xMax),
        Mathf.Clamp(transform.position.y, boundary.yMin , boundary.yMax),
        transform.position.z
    );
}

void OnDrawGizmos()
{

        if (!DrawBoundary)
            return;

        Gizmos.color = Color.cyan;
        Gizmos.DrawLine(new Vector2(ScreenBounds.xMin, ScreenBounds.yMin), new Vector2(ScreenBounds.xMax, ScreenBounds.yMin));
        Gizmos.DrawLine(new Vector2(ScreenBounds.xMax, ScreenBounds.yMin), new Vector2(ScreenBounds.xMax, ScreenBounds.yMax));

        Gizmos.DrawLine(new Vector2(ScreenBounds.xMin, ScreenBounds.yMax), new Vector2(ScreenBounds.xMin, ScreenBounds.yMin));
        Gizmos.DrawLine(new Vector2(ScreenBounds.xMax, ScreenBounds.yMax), new Vector2(ScreenBounds.xMin, ScreenBounds.yMax));

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.