[英]Scaling a 2d game object with mouse drag in unity
所以我对此进行了一些尝试和发布,但仍然没有成功。 我想要的行为是这样的:
有一个圆圈(球体),我希望玩家能够单击并向外拖动它以使其变大,或向内拖动以使其变小。 以前,我根据初始拖动点缩放对象,但这使我无法将形状缩小回缩..只能让它变大。 我想我需要根据相对于形状中心的阻力方向来计算我们是应该放大还是缩小。 为了弄清楚这一点,我们需要检查鼠标的位置,同时拖动,将其与前一帧进行比较,并决定我们是离中心更近还是更远。 如果我们更近,那么我们正在向内拖动,我们应该缩小对象(使其变小)。 如果我们离得更远,那么我们正在向外移动,应该扩大规模(扩大规模)。 我有几个问题。 我似乎根本无法让圆圈缩小。 拖动方向的检测似乎有缺陷,因为似乎我的 isDraggingOut 布尔值基本上总是正确的,我不明白为什么。 我遇到的另一个问题是,每次单击圆圈时,它都会恢复到初始大小。
我正在尝试在我的代码中执行以下步骤:
任何帮助将不胜感激,因为我真的很挣扎于此。
{
public Vector3 temp;
public Vector3 dragStartingPoint;
public Vector3 centreOfShape;
public Vector3 currentDragPosition;
public Vector3 previousDragPosition;
public bool isDraggingOut;
public float previousDistance;
public float currentDistance;
public float amountDragged;
public float clampedAmountDragged;
private void Awake()
{
centreOfShape = transform.position;
}
private void OnMouseDown()
{
dragStartingPoint = Input.mousePosition;
}
private void OnMouseDrag()
{
//check where current position is
currentDragPosition = (Input.mousePosition);
//check distance of previous position
previousDistance = Vector3.Distance(previousDragPosition, centreOfShape);
//check distance of current position
currentDistance = Vector3.Distance(currentDragPosition, centreOfShape);
//determine whether player is dragging in or out. If current distance is
// more than the distance in the previous frame, we are dragging out
if (currentDistance > previousDistance)
{
isDraggingOut = true;
}
else
{
isDraggingOut = false;
}
if (isDraggingOut == true)
{
amountDragged = Vector3.Distance(currentDragPosition, dragStartingPoint);
// set minimum and maximum drag amount and store in clampedAmountDragged variable
clampedAmountDragged = Mathf.Clamp(amountDragged, 100f, 300f);
// set amountDragged to clampedAmount to apply minimum and maximum
amountDragged = clampedAmountDragged;
temp = transform.localScale;
temp.x = amountDragged / 100;
temp.y = amountDragged / 100;
//make scale of object equal to temp variable
transform.localScale = temp;
}
// else we are dragging in
else if (isDraggingOut == false)
{
amountDragged = Vector3.Distance(currentDragPosition, dragStartingPoint);
// set minimum and maximum drag amount and store in clampedAmountDragged variable
clampedAmountDragged = Mathf.Clamp(amountDragged, 100f, 300f);
// set amountDragged to clampedAmount to apply minimum and maximum
amountDragged = clampedAmountDragged;
amountDragged = amountDragged - 300;
amountDragged = Mathf.Abs(amountDragged);
temp = transform.localScale;
temp.x = amountDragged / 100;
temp.y = amountDragged / 100;
//make scale of object equal to temp variable
transform.localScale = temp;
}
currentDragPosition = previousDragPosition;
}
}```
首先,您需要明确您的游戏是在 2d 还是 3d 空间中。注意Input.mousePosition
返回屏幕空间上的 2d 位置,而不是世界空间。 我相信这就是球体大小正在重置的原因。 您可能正在寻找Camera.main.ScreenToWorldPoint(Input.mousePosition)
。
这将为您提供世界空间光标位置。 将CenterOfShape
和Camera.main.ScreenToWorldPoint(Input.mousePosition)
之间的距离存储在OnMouseDown
。然后在OnMouseDrag()
,进行新的距离检查并找出新距离与之前存储的距离的差异。 取这个差异并将其均匀地添加到球体的比例中。
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.