Box Collider在Unity中重叠2D

时间:2017-08-12 23:46:33

标签: unity3d unity3d-2dtools

我有两个带有盒式对撞机的游戏对象。一个是玩家,以固定的速度向另一个移动。碰撞使玩家停止(Poo角色; D)

但是它们重叠,你可以在这里看到它: enter image description here

我不知道为什么会这样。与顶部或底部碰撞工作正常...从左侧发生相同的效果。绿色块只有一个对撞机,没有RigidBody。

的Gif: enter image description here

另一个Gif,带有MovePosition()......从顶部作品中碰撞,但是"重新进入"停止角色。为什么?!: enter image description here

  1. GIF,上下移动是好的,在块顶部左右移动使他慢下来。奇怪的... enter image description here
  2. 运动脚本:

    public class PlayerController : MonoBehaviour
    {
        public float Speed = 10f;
    
        private Rigidbody2D rb2D;
    
        private Vector2 DirectionLeft;
        private Vector2 DirectionRight;
        private Vector2 DirectionUp;
        private Vector2 DirectionDown;
    
        private Vector2 CurrentDirection;
    
        // Use this for initialization
        void Start()
        {
            rb2D = GetComponent<Rigidbody2D>();
    
            DirectionLeft = new Vector2(Speed*-1, 0);
            DirectionRight = new Vector2(Speed, 0);
            DirectionUp = new Vector2(0, Speed * -1);
            DirectionDown = new Vector2(0, Speed);
    
            CurrentDirection = DirectionLeft;
        }
    
        void SetAnimationDirection()
        {
            Vector3 scale = transform.localScale;
    
            if (CurrentDirection == DirectionLeft)
                scale.x = 1;
            else
                scale.x = -1;
    
            transform.localScale = scale;
        }
    
        void FixedUpdate()
        {
            float moveHorizontal = Input.GetAxis("Horizontal");
            float moveVertical = Input.GetAxis("Vertical");
    
            if (moveHorizontal > 0)
                CurrentDirection = DirectionRight;
            if (moveHorizontal < 0)
                CurrentDirection = DirectionLeft;
            if (moveVertical < 0)
                CurrentDirection = DirectionUp;
            if (moveVertical > 0)
                CurrentDirection = DirectionDown;
    
            Debug.Log(CurrentDirection);
            SetAnimationDirection();
    
            rb2D.velocity = CurrentDirection;
        }
    

1 个答案:

答案 0 :(得分:1)

解决第一个问题(重叠):

我使用transform.localScale = scale来更改每个FixedUpdate()精灵的方向,也忘了更新比例因子,因为我添加了一个新的扩展,它将我的对象的大小调整了1个Unity单元。

另一个问题(卡在边缘)解决了将Collider 2D的 Edge Radius 设置为低于1.0f(例如0.09f)以及< strong>调整边界框的大小。这将防止对象卡在边缘,因为边界框边缘现在是圆角的。