簡體   English   中英

如何在單擊鼠標時禁用對象控制?

[英]How can I disable Object control on mouse click?

當用戶移動鼠標指針然后單擊時,它是x軸對象。 物體以相同的鼠標指針位置落在地面上。 但是當物體掉落時,如果用戶移動指針的位置,也會影響物體掉落的位置。 我只希望物體落在用戶單擊的位置上,而當物體落在地面上時不控制物體。 碼:

public class Ball : MonoBehaviour {

    Rigidbody2D body;
    float mousePosInBlocks;

    void Start () {
        body = GetComponent<Rigidbody2D> ();
        body.isKinematic = true;

    }

    void Update () {

        if (Input.GetMouseButtonDown (0)) {

            body.isKinematic = false;
        }

        Vector3 ballPos = new Vector3 (0f, this.transform.position.y, 0f);
        mousePosInBlocks = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;


        //not go outside from border
        ballPos.x = Mathf.Clamp (mousePosInBlocks, -2.40f, 2.40f);

        body.position = ballPos;
    }
}

我的評論如下。 出現鼠標單擊后,退出該功能即可。 確實沒有什么比這更多的了。

public class Ball : MonoBehaviour {

    Rigidbody2D body;
    float mousePosInBlocks;
    bool wasDropped = false;

    void Start () {
        body = GetComponent<Rigidbody2D> ();
        body.isKinematic = true;
    }

    void Update () {
        //Don't do anything after the ball is dropped.
        if(wasDropped)
            return;

        if (Input.GetMouseButtonDown (0)) {
            body.isKinematic = false;
            wasDropped = true;
        }

        Vector3 ballPos = new Vector3 (0f, this.transform.position.y, 0f);
        mousePosInBlocks = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
        //not go outside from border
        ballPos.x = Mathf.Clamp (mousePosInBlocks, -2.40f, 2.40f);

        body.position = ballPos;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM