简体   繁体   English

如何使运动学刚体检测与 tilemap collision 2d 的碰撞?

[英]How to make kinematic rigidbody detect collisions with tilemap collision 2d?

I'm making PacMan type game, just for fun and i have a problem.我正在制作 PacMan 类型的游戏,只是为了好玩,但我遇到了问题。 I've created a character and made a map with tilemaps.我创建了一个角色并制作了一个 map 瓦片地图。 I added tilemap collider 2d to tilemap and box collider 2d and rigidbody(kinematic) for character.我为角色添加了 tilemap collider 2d 和 box collider 2d 和 rigidbody(kinematic)。 Here is my code for movement:这是我的运动代码:

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

I've changed the "Contact pairs mode" but it didn't work.我已经更改了“联系人对模式”,但它没有用。 Here is the photo of my problem: collision problem这是我的问题的照片:碰撞问题

Kinematic rigidbodies don't allow collision.运动学刚体不允许碰撞。 They are meant to be used for walls and things.它们旨在用于墙壁和其他东西。 It would be better to use a dynamic Rigidbody2D, and disable gravity and any other forces you don't want.最好使用动态Rigidbody2D,并禁用重力和您不想要的任何其他力。

A dynamic Rigidbody is one of the other things you can select instead of kinematic in the drop down menu.动态刚体是您可以 select 而不是下拉菜单中的运动学的其他内容之一。 It is very important that you set it to dynamic, because dynamic allows there to be forces on the object.将其设置为动态非常重要,因为动态允许在 object 上施加力。

Also, when using a Rigidbody, you don't want to move it using transform.此外,在使用刚体时,您不想使用变换来移动它。

I would move it with velocity so it detects collision.我会以速度移动它,以便它检测到碰撞。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM