繁体   English   中英

使用鼠标拖动统一缩放二维游戏对象

[英]Scaling a 2d game object with mouse drag in unity

所以我对此进行了一些尝试和发布,但仍然没有成功。 我想要的行为是这样的:

有一个圆圈(球体),我希望玩家能够单击并向外拖动它以使其变大,或向内拖动以使其变小。 以前,我根据初始拖动点缩放对象,但这使我无法将形状缩小回缩..只能让它变大。 我想我需要根据相对于形状中心的阻力方向来计算我们是应该放大还是缩小。 为了弄清楚这一点,我们需要检查鼠标的位置,同时拖动,将其与前一帧进行比较,并决定我们是离中心更近还是更远。 如果我们更近,那么我们正在向内拖动,我们应该缩小对象(使其变小)。 如果我们离得更远,那么我们正在向外移动,应该扩大规模(扩大规模)。 我有几个问题。 我似乎根本无法让圆圈缩小。 拖动方向的检测似乎有缺陷,因为似乎我的 isDraggingOut 布尔值基本上总是正确的,我不明白为什么。 我遇到的另一个问题是,每次单击圆圈时,它都会恢复到初始大小。

我正在尝试在我的代码中执行以下步骤:

  1. 鼠标拖动时:检查鼠标当前位置
  2. 检查鼠标先前位置的距离
  3. 检查当前位置的距离
  4. 确定玩家是拖入还是拖出。 如果当前距离大于前一帧中的距离,我们正在拖出
  5. 如果为真,则计算拖动起点与鼠标当前位置之间的距离,并相应缩放
  6. 如果距离小于前一帧,那么我们正在拖入
  7. 然后计算拖动起点和鼠标当前位置之间的距离,并相应地缩放(在这里,我试图应用放大计算的逆计算 - 这可能是错误的)
  8. 我在代码中做的最后一件事是使当前位置等于 previousPosition 变量,以便在下一帧中可以比较它。

任何帮助将不胜感激,因为我真的很挣扎于此。

{
    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)

这将为您提供世界空间光标位置。 CenterOfShapeCamera.main.ScreenToWorldPoint(Input.mousePosition)之间的距离存储在OnMouseDown 。然后在OnMouseDrag() ,进行新的距离检查并找出新距离与之前存储的距离的差异。 取这个差异并将其均匀地添加到球体的比例中。

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM