为什么多边形对撞机2d会限制玩家的移动?

时间:2020-07-18 06:25:34

标签: c# unity3d game-physics game-development physics-engine

我有一朵云,上面附着了多边形对撞机2d。另外,我有一个附带盒对撞机2d的播放器。当玩家降落在云上时,在移动过程中的某个时刻,某种东西正在阻止他。他在做动画,但不动。

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

Player & cloud colliders

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

有什么解决方法吗?

下面是我的代码:

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

Animation nodes

1 个答案:

答案 0 :(得分:0)

我认为这是因为您的角色的碰撞器被卡在云的多边形碰撞器的颠簸部分。 一种解决方案是将角色的对撞机更改为半胶囊对撞机或胶囊对撞机,以便角色可以在某些粗糙表面上平稳行走。