2D角色在Unity中粘在墙上

问题描述

在我编写破折号脚本之后,它开始发生。现在,每当我冲进墙壁时,我的角色都会卡在那堵墙壁上。如果我冲向屋顶,那么我可以例如走在屋顶上。另外,当我在墙旁/地板上冲破时,只要按一下,它就不会停止冲破。任何帮助将不胜感激,谢谢。 下面的代码是破折号脚本,如果需要,我也可以发布变量集和我的字符控制器脚本。

void Start()
    {
        _rigidbody = GetComponent<Rigidbody2D>();

        dashTime = baseDashTime;
    }

    void Update()
    {
        isGrounded = Physics2D.OverlapCircle(feet.position,checkRadius,checkGround);

        if(isGrounded == true)
        {
            dashTime = baseDashTime;
        }
        if (direction == 0)
        {
            if (Input.GetKey(KeyCode.W) && (Input.GetKeyDown(KeyCode.LeftShift)))
            {
                direction = 1;
            }
            else if (Input.GetKey(KeyCode.A) && (Input.GetKeyDown(KeyCode.LeftShift)))
            {
                direction = 2;
            }
            else if (Input.GetKey(KeyCode.S) && (Input.GetKeyDown(KeyCode.LeftShift)))
            {
                direction = 3;
            }
            else if (Input.GetKey(KeyCode.D) && (Input.GetKeyDown(KeyCode.LeftShift)))
            {
                direction = 4;
            }
        }
        else
        {
            if (dashTime <= 0)
            {
                direction = 0;
                _rigidbody.veLocity = Vector2.zero;
            }
            else
            {
                dashTime -= Time.deltaTime;
                if (direction == 1)
                {
                    _rigidbody.veLocity = Vector2.up * dashSpeed;
                }
                else if (direction == 2)
                {
                    _rigidbody.veLocity = Vector2.left * dashSpeed;
                }
                else if (direction == 3)
                {
                    _rigidbody.veLocity = Vector2.down * dashSpeed;
                }
                else if (direction == 4)
                {
                    _rigidbody.veLocity = Vector2.right * dashSpeed;
                }
            }
        }
    }

解决方法

在您的代码中,更新功能中包含以下语句。当您在地面上冲刺时,我猜测IsGrounded会保持不变,因此玩家将永远冲刺。这可能是导致玩家卡住的原因。

 if(isGrounded == true)
    {
        dashTime = baseDashTime;
    }