Unity - 运行 像 sonic 一样循环而不会掉下来

Unity - Run a loop like sonic without falling down

我正在为一个自由职业者制作这个 2D 平台游戏,我必须做一个音速机制,我必须让角色 运行 通过一个完整的循环而不掉下来,我意识到要做到这一点首先,我必须在循环中旋转符合他 运行 的角色,但第二部分是我卡住的地方。

所以,基本上,我怎样才能让角色运行在不掉下来的情况下循环。

private void OnCollisionEnter2D(Collision2D coll)
{
    Vector3 collRotation = coll.transform.rotation.eulerAngles;

    if (coll.gameObject.tag == "Ground")
    {
    //in this part i rotate the player as he runs through the loop
        transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, collRotation.z);

        //this is the part that i am stuck, trying to figure out how to make the character stay in the loop without falling, i tried collision detection,
    //i tried raycasting but nothing seems to work
        if (IsGrounded)
        {
            GetComponent<Rigidbody2D>().AddForce(Vector2.down, ForceMode2D.Impulse);
        }
    }
}

最常用于重制和类似游戏的技巧是获取玩家角色在进入时的速度。

// Feel free to adjust this to whatever works for your project.
const float minimumAttachSpeed = 2f;

// This should be your characters current movement speed
float currentSpeed;

// You need a Rigidbody in this example, or you can just disable
// any custom gravity solution you may have created
Rigidbody2D rb;

如果角色的速度超过最低附着速度,您可以让他们以该速度遵循预定义的路径。

bool LoopAttachmentCheck()
{
    return minimumAttachSpeed <= currentSpeed;
}

现在您可以检查自己的移动速度是否足够快!对于此示例,我假设您使用的是 Rigidbody2D...

(当您正在进入或当前处于循环中时,此检查只应 运行)

void Update()
{
    if( LoopAttachmentCheck() )
    {
        // We enable this to prevent the character from falling
        rb.isKinematic = true;

        // Here you write the code that allows the character to move
        // in a circle, e.g. via a bezier curve, at the currentSpeed.
    }
    else
    {
        rb.isKinematic = false;
    }
}

由您来实现实际的旋转行为。如果我是你,一个好的方法是(假设它是一个完美的圆)从圆心使用 RotateAround

如果您有更复杂的形状,您可以使用 waypoints 进行移动,然后按照您的速度遍历它们。

一旦你跟不上速度,你的角色就会掉下来(例如,如果玩家决定停止 运行ning)并且 Rigibody2D 会变得运动。

希望对您有所帮助!