简体   繁体   English

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

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

So I have had a few attempts and posts about this but have still not been successful.所以我对此进行了一些尝试和发布,但仍然没有成功。 The behaviour I want is this:我想要的行为是这样的:

There is a circle (sphere) I want the player to be able to click and drag it outwards to make it larger, or drag inwards to make it smaller.有一个圆圈(球体),我希望玩家能够单击并向外拖动它以使其变大,或向内拖动以使其变小。 Previously, I was scaling the object based on the inital dragging point, but this was giving me no way to scale the shape back down.. only make it larger.以前,我根据初始拖动点缩放对象,但这使我无法将形状缩小回缩..只能让它变大。 I figured I need to calculate whether we should be scaling up or down, based on the direction of the drag in relation to the centre of the shape.我想我需要根据相对于形状中心的阻力方向来计算我们是应该放大还是缩小。 To figure this out, we need to check the position of the mouse, while dragging, compare it to the previous frame and decide whether we are closer or further away from the centre.为了弄清楚这一点,我们需要检查鼠标的位置,同时拖动,将其与前一帧进行比较,并决定我们是离中心更近还是更远。 If we are closer, then we are dragging inwards and we should scale the object down (make it smaller).如果我们更近,那么我们正在向内拖动,我们应该缩小对象(使其变小)。 Id we are further away, then we are moving outwards and should be scaling up (make it larger).如果我们离得更远,那么我们正在向外移动,应该扩大规模(扩大规模)。 I'm having a couple of problems.我有几个问题。 I don't seem to be able to get the circle to scale down at all.我似乎根本无法让圆圈缩小。 The detection for the direction of the drag seems to be flawed as it seems my bool for isDraggingOut is basically always true, I can't understand why.拖动方向的检测似乎有缺陷,因为似乎我的 isDraggingOut 布尔值基本上总是正确的,我不明白为什么。 The other problem I'm having is that every time I click the circle, it snaps back to it's initial size.我遇到的另一个问题是,每次单击圆圈时,它都会恢复到初始大小。

I am trying to take the following steps in my code:我正在尝试在我的代码中执行以下步骤:

  1. On Mouse drag: check where current position of the mouse is鼠标拖动时:检查鼠标当前位置
  2. check distance of previous position of the mouse检查鼠标先前位置的距离
  3. check distance of current position检查当前位置的距离
  4. determine whether player is dragging in or out.确定玩家是拖入还是拖出。 If current distance is more than the distance in the previous frame, we are dragging out如果当前距离大于前一帧中的距离,我们正在拖出
  5. If this is true, then calculate the distance between the drag start point and the current position of the mouse, and scale accordingly如果为真,则计算拖动起点与鼠标当前位置之间的距离,并相应缩放
  6. If the distance is less than the previous frame then we are dragging in如果距离小于前一帧,那么我们正在拖入
  7. then calculate the distance between the drag start point and the current position of the mouse, and scale accordingly (here, i have tried to apply an inverse calculation of the upscaling calculation - this could be wrong)然后计算拖动起点和鼠标当前位置之间的距离,并相应地缩放(在这里,我试图应用放大计算的逆计算 - 这可能是错误的)
  8. The last thing I do in my code is make the current position equal to previousPosition variable, so that in the next frame it can be compared.我在代码中做的最后一件事是使当前位置等于 previousPosition 变量,以便在下一帧中可以比较它。

Any help would be appreciated as I am really struggling with this one.任何帮助将不胜感激,因为我真的很挣扎于此。

{
    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;
    }
}```

Firstly you need to make it clear if your game is in 2d or 3d space.Note that Input.mousePosition returns the 2d position on screen space, not world space.首先,您需要明确您的游戏是在 2d 还是 3d 空间中。注意Input.mousePosition返回屏幕空间上的 2d 位置,而不是世界空间。 I believe this is why the sphere's size is resetting.我相信这就是球体大小正在重置的原因。 You are probably looking for Camera.main.ScreenToWorldPoint(Input.mousePosition) .您可能正在寻找Camera.main.ScreenToWorldPoint(Input.mousePosition)

This will give you your world space cursor position.这将为您提供世界空间光标位置。 Store the distance between CenterOfShape and Camera.main.ScreenToWorldPoint(Input.mousePosition) in OnMouseDown .Then In OnMouseDrag() , do a new distance check and find difference the the new distance with the one stored earlier.CenterOfShapeCamera.main.ScreenToWorldPoint(Input.mousePosition)之间的距离存储在OnMouseDown 。然后在OnMouseDrag() ,进行新的距离检查并找出新距离与之前存储的距离的差异。 take this difference and add it uniformly to the sphere's scale.取这个差异并将其均匀地添加到球体的比例中。

I hope this helped.我希望这有帮助。

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

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