使用rigidbody2D拖动对象时,它会穿过碰撞器(墙壁)

时间:2015-08-26 19:49:01

标签: unity3d unityscript unity5

好的,所以我制作这个游戏,用户可以在屏幕上拖动球,但它不应该离开游戏区。我虽然得到了following problem,当我把它推向碰撞器时它反弹回来,如果我用力太猛,它就会离开屏幕(我需要让它不要离开屏幕。用户可以随意拖动它,但当然在屏幕内。)

有关如何解决此问题的任何提示?

以下是我正在使用的拖动代码:

using UnityEngine;
using System.Collections;

public class CircleManager : MonoBehaviour {


        private bool dragging = false;

        private Vector3 screenPoint;
        private Vector3 offset;

        // Pressionando
        void OnMouseDown()
        {
                dragging = true;
                screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
                offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        }

        // Arrastando
        void OnMouseDrag()
        {
                Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;

                //i tried with both below.
                //transform.position = cursorPosition;
                transform.GetComponent<Rigidbody2D>().MovePosition(cursorPosition);
        }

        // Soltando
        void OnMouseUp()
        {
                dragging = false;
        }


}

谢谢!

0 个答案:

没有答案