我正在尝试限制屏幕上对象的可拖动区域,并且我的代码中出现了一些错误 - 我现在试图保持简单,只需重置最大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);
}
答案 0 :(得分:2)
您正尝试将Vector3
分配给float
这里
transform.position.x = new Vector3(maxDragX, mousePosition.y, distance);
在这里
transform.position.y = new Vector3(mousePosition.x, maxDragY, distance);
答案 1 :(得分:2)
你不能为position.x或position.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 = new Vector3(maxDragX, mousePosition.y, distance);
if(transform.position.y >= maxDragY)
transform.position = new Vector3(mousePosition.x, maxDragY, distance);
}