【问题标题】:How to make kinematic rigidbody detect collisions with tilemap collision 2d?如何使运动学刚体检测与 tilemap 碰撞 2d 的碰撞?
【发布时间】:2021-06-16 21:26:15
【问题描述】:

我正在制作 PacMan 类型的游戏,只是为了好玩,但我遇到了问题。我已经创建了一个角色并使用 tilemaps 制作了一张地图。我将 tilemap collider 2d 添加到 tilemap 和 box collider 2d 以及用于角色的刚体(运动学)。这是我的移动代码:

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private float _speed = 3.0f;
    private Vector2 _direction = Vector2.zero;


    private void Start()
    {
     
    }

    private void Update()
    {
        Move();

        CheckInput();
    }

    private void CheckInput()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            _direction = Vector2.left;
        } else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            _direction = Vector2.right;
        } else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            _direction = Vector2.up;
        } else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            _direction = Vector2.down;
        }
    }

    private void Move()
    {
        transform.localPosition += (Vector3)(_direction * _speed) * Time.deltaTime;
    }
}

我已更改“联系人配对模式”,但它不起作用。这是我的问题的照片: collision problem

【问题讨论】:

    标签: c# unity3d 2d tile


    【解决方案1】:

    运动学刚体不允许碰撞。它们旨在用于墙壁和东西。最好使用 dynamic Rigidbody2D,并禁用重力和任何其他您不想要的力。

    动态刚体是您可以在下拉菜单中选择的其他项目之一,而不是运动学。将其设置为动态非常重要,因为动态允许对对象施加力。

    此外,当使用刚体时,您想使用变换来移动它。

    我会以速度移动它,以便它检测到碰撞。

    Rigidbody2D rb;
    private void Start()
    {
        rub = GetComponent<Rigidbody2D>();
    }
    
    private void Update()
    {
        Move();
    
        CheckInput();
    }
    
    private void CheckInput()
    {
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            _direction = Vector2.left;
        } else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            _direction = Vector2.right;
        } else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            _direction = Vector2.up;
        } else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            _direction = Vector2.down;
        }
    }
    
    private void Move()
    {
        rb.velocity = _direction * _speed * Time.deltaTime;
        //set all of the drag variables on the Rigidbody
        //to very high, so it slows down when they stop moving.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      相关资源
      最近更新 更多