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

时间:2015-11-26 19:18:31

标签: c# unity3d

当用户移动鼠标指针然后单击时,它是x轴对象。物体落在相同鼠标指针位置的地面上。但是当对象移动时,如果用户移动指针的位置,它也会影响对象的下落位置。我只希望对象落在用户点击的位置,并且当落在地面上时不控制对象。 代码:

<anon>:75:29: 75:31 error: the trait `core::marker::Sized` is not implemented for the type `core::ops::Fn(i32, i32) + 'static` [E0277]
<anon>:75     fn apply_bin(&mut self, op: Fn(i32,i32)) -> Result<i32, String> {
                                      ^~

1 个答案:

答案 0 :(得分:0)

我评论中的Folloup。单击鼠标后退出该功能。它确实没有比这更多的东西了。

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