2

我一直在使用 Unity 5 引擎研究一些示例来开发无尽的跑步游戏。我对我的角色应用了跳跃动作。它确实工作得很好,但我希望它有点完美,所以我在这个例子中使用曲线植入了跳跃,遇到了这个问题。那是在为角色控制器应用曲线并进行一些调整之后,现在当我跳跃时,在我接触平台(地面)后曲线开始调整控制器,这使得跳跃变得不切实际。我确实尝试使用固定的更新方法来实现跳跃,因为游戏是一个无尽的跑步者,它基本上会逐帧更新所有内容,但它不起作用。如何实现逼真的跳跃?以下是我到目前为止所尝试的。

if (controller.isGrounded)
    {
        verticalVelocity = -0.5f; //upward thrust / jump height
            if (currentBaseState.fullPathHash == locoState) // is character in moving state
            {
                if (Input.GetButtonDown("Jump"))
                {
                    verticalVelocity = 18f;
                    anim.SetBool("Jump", true);
                }
            }
            else if (currentBaseState.fullPathHash == jumpState) //Is character in jump state
            {
                if (!anim.IsInTransition(0))
                {
                    if (useCurves)
                    {
                        controller.height = anim.GetFloat("ColliderHeight"); //get the controller height using curves
                        controller.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f); //Get the controller Y axis using the curves (center of chr ctrlr)
                    }

                    anim.SetBool("Jump", false);

                }
    // Applying curves
                Ray ray = new Ray(transform.position + Vector3.up, -Vector3.up);
                RaycastHit hitInfo = new RaycastHit();

                if (Physics.Raycast(ray, out hitInfo))
                {
                    print(ray.ToString());
                    if (hitInfo.distance > 1.75f)
                    {
                        anim.MatchTarget(hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(0, 1f, 0), 0), 0.03f, 0.6f);
                    }
                }    

            }
    }

字符在开始时跳跃 字符在开始时跳跃

触地的字符控制器 触地的字符控制器

接触地面后的结果 接触地面后的结果

帮助将不胜感激

4

2 回答 2

1

您根据曲线调整控制器的代码位于 if 语句中,询问角色是否接地。

if (controller.isGrounded)
{
 ...
}

这样它只会在角色接触地面时进行调整。

于 2016-07-07T04:49:45.110 回答
0

你的游戏对象有刚体吗?如果不是,那么尝试添加刚体并将其设置为 iskinematic,因为当您移动具有碰撞器且没有刚体的对象时,需要重新计算完整的静态碰撞数据,这很慢。或者,可能是,尝试玩动画跳帧率不要让它太小

于 2016-07-07T05:04:11.947 回答