简体   繁体   English

如何在单击鼠标时禁用对象控制?

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

It's x-axis object when user move the mouse pointer and then click. 当用户移动鼠标指针然后单击时,它是x轴对象。 Object falls on the ground at the same mouse pointer position. 物体以相同的鼠标指针位置落在地面上。 But when object is falling if user move the position of pointer it also affect the object falling position. 但是当物体掉落时,如果用户移动指针的位置,也会影响物体掉落的位置。 I only want object falls on that position where user click and does not control the object when falling on the ground. 我只希望物体落在用户单击的位置上,而当物体落在地面上时不控制物体。 Code: 码:

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

Folloup from my comment. 我的评论如下。 Just exit out of the function after the mouse click appeared. 出现鼠标单击后,退出该功能即可。 There really isn't more to it than that. 确实没有什么比这更多的了。

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