簡體   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