首页 > 解决方案 > Unity:玩家离墙太近“无视”重力 [视频]

问题描述

我有一个小平台游戏,玩家可以在其中跳跃。跳跃效果很好,玩家在空中跳得很高,然后根据我设置的重力跌落下来。然而,一旦玩家越过他可能跌倒的边缘,他的物理似乎完全卡住了。他没有倒下,而是慢慢地倒下,好像他是用树叶做成的。

我发现一旦玩家撞到侧墙,也会发生这种情况。看起来一旦你越过边缘,玩家就会触摸墙壁的一侧一秒钟,这会破坏它的物理特性。

我在这里记录了这个问题:

https://youtu.be/CE-W4wmMqcA

这些是我的播放器设置:

在此处输入图像描述

我还尝试将 2D 物理添加到墙壁和 blayer,并将所有内容设置为 0。这确实改变了一些效果,但远未解决它......

另外,我的固定更新,我跳的地方(从更新中捕获布尔,在固定中做物理:)

void FixedUpdate()
{
    currentPlayerSpeed = rb.velocity.x;

    if (moveLeft)
    {
        rb.AddForce((Vector2.left * movementSpeed * Time.deltaTime) - rb.velocity, ForceMode2D.Force);

    }
    if (moveRight)
    {
        rb.AddForce((Vector2.right * movementSpeed * Time.deltaTime) - rb.velocity, ForceMode2D.Force);
    }

    if (jump)
    {

        if (isGrounded)
        {
            isGrounded = false;

            rb.AddForce(Vector2.up * (jumpHeight * counterForJumpHeight) * Time.deltaTime, ForceMode2D.Impulse);
            jump = false;

            anim.SetBool("bool_anim_isJumping", true);
        }

        if (timer != null)
            timer.Stop();

        counterForJumpHeight = jumpMulitMin;

        jumpAlreadCharging = false;

    }

    if (!moveLeft && !moveRight) // if no movement input is happening
    {
        if (isGrounded)
        {
            StopVelocity();
        }

    }
}

标签: unity3d

解决方案


好的,我想我知道当你跌倒时,你的变量moveLeftmoveRight变量都是假的,所以你正在调用StopVelocity并且这个 if 语句被执行。

if (!moveLeft && !moveRight) // if no movement input is happening
{
    if (isGrounded)
    {
        StopVelocity();
    }

}

这会导致这种奇怪的行为。因为你没有跳但只有跌倒isGrounded也是真的:)


推荐阅读