限制X和Y可拖动区域

本文关键字:拖动 区域 限制 | 更新日期: 2023-09-27 18:04:08

我试图限制屏幕上对象的可拖动区域,我的代码中出现了一些错误-我试图保持简单,只是重置最大x或y,如果对象被拖动超出这些限制,但我仍然没有任何成功。我真的需要一些帮助来理解如何做这件事。

    float maxDragX = 1000;
    float maxDragY = 700;
    Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, distance);
    transform.position = mousePosition;                                 // set object coordinates to mouse coordinates
    if(transform.parent.gameObject == partsPanel)
    {
        transform.SetParent(dragLayer.transform);                       // pop object to draglayer to move object out of partsPanel
    }
    if(transform.parent.gameObject == buildBoard)
    {
        // Constrain drag to boundaries of buildBoard Code
        if(transform.position.x >= maxDragX)
            transform.position.x = new Vector3(maxDragX, mousePosition.y, distance);
        if(transform.position.y >= maxDragY)
            transform.position.y = new Vector3(mousePosition.x, maxDragY, distance);
    }

限制X和Y可拖动区域

不能设置位置向量。X或位置。它们只是浮动的。你必须完全改变位置

 float maxDragX = 1000;
    float maxDragY = 700;
    Vector3 mousePosition = new Vector3(eventData.position.x, eventData.position.y, distance);
    transform.position = mousePosition;                                 // set object coordinates to mouse coordinates
    if(transform.parent.gameObject == partsPanel)
    {
        transform.SetParent(dragLayer.transform);                       // pop object to draglayer to move object out of partsPanel
    }
    if(transform.parent.gameObject == buildBoard)
    {
        // Constrain drag to boundaries of buildBoard Code
        if(transform.position.x >= maxDragX)
            transform.position = new Vector3(maxDragX, mousePosition.y, distance);
        if(transform.position.y >= maxDragY)
            transform.position = new Vector3(mousePosition.x, maxDragY, distance);
    }

您正在尝试将Vector3分配给float

这里

transform.position.x = new Vector3(maxDragX, mousePosition.y, distance);

transform.position.y = new Vector3(mousePosition.x, maxDragY, distance);