首页 > 解决方案 > 为什么多边形对撞机 2d 会限制玩家的移动?

问题描述

我有一个附有多边形对撞机 2d 的云。另外,我有一个附加了 2d 盒对撞机的玩家。当玩家降落在云上时,在移动过程中的某个时刻,有什么东西阻止了他。他正在制作动画,但他一动不动。

下面是我的对撞机的图像:

播放器和云对撞机

当我开始运行游戏时,他左右移动。所以我认为这不是代码问题。在某一时刻,他被卡在上面的位置,他不能向右移动,但他可以向左移动。我猜多边形对撞机正在阻止他自由移动。当我回去时,他正在走路,当到达上述位置时,他无法前进。

有什么解决方法吗?

下面是我的代码:

public class Player : MonoBehaviour
{
    public float speed = 7f;
    public float maxVelocity = 8f;

    private Rigidbody2D rb;
    private Animator anim;


    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        MovePlayerUsingKeyboard();   
    }

    public void MovePlayerUsingKeyboard()
    {
        float forceX = 0f;
        float velocity = Mathf.Abs(rb.velocity.x);
        Debug.Log("Player Velocity : " + velocity);

        float direction = Input.GetAxis("Horizontal");

        if (direction < 0)
        {
            if (maxVelocity > velocity)
            {
                anim.SetBool("Walk", true);
                forceX = -speed;                
            }
            //Changing the direction the player faces
            Vector3 temp = transform.localScale;
            temp.x = -1.3f;
            transform.localScale = temp;
        }
        else if (direction > 0)
        {
            if (maxVelocity > velocity)
            {
                anim.SetBool("Walk", true);
                forceX = speed;
            }
            Vector3 temp = transform.localScale;
            temp.x = 1.3f;
            transform.localScale = temp;
        }
        else
        {
            anim.SetBool("Walk", false);
        }

        rb.AddForce(new Vector2(forceX, 0));
    }
}

动画节点

标签: c#unity3dgame-physicsgame-developmentphysics-engine

解决方案


我认为这是因为您角色的对撞机卡在了云多边形对撞机的颠簸部分。一个解决办法是把角色的对撞机改成半胶囊对撞机或者胶囊对撞机,让角色可以在一些粗糙的表面上平稳行走。


推荐阅读