gpt4 book ai didi

c# - 约束 X 和 Y 可拖动区域

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:48 25 4
gpt4 key购买 nike

我试图限制屏幕上某个对象的可拖动区域,但我的代码中出现了一些错误 - 我现在试图保持简单,如果对象被拖到这些限制之外,但我仍然没有取得任何成功。我真的需要一些帮助来理解如何做到这一点。

    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);
}

最佳答案

您不能为 position.x 或 position.y 设置向量,它们只是 float 的。你必须完全改变立场

 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);
}

关于c# - 约束 X 和 Y 可拖动区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com