【发布时间】:2018-02-17 17:37:41
【问题描述】:
我正在 Unity3d 中开发一个无尽的跑步游戏,我遇到了这个问题。我已经让我的角色跳跃,在这样做的过程中,我使用了 Unity 中的曲线功能来改变跳跃时的角色高度和中心。
然后我遇到了这个问题。当我按下跳转按钮时,我使动画剪辑以没有向上推力或任何物理的方式运行。只是我正在减少对撞机的高度和中心点。但这样做时,我的角色往往会因为我实施了重力而下降,因为最终我的角色应该会下降。
我唯一不想涉及重力的情况是我在跳跃时(跳跃动画正在运行时)。我该怎么做呢。或有关如何解决此错误的任何建议?
下面是我为跳跃实现的代码。
private float verticalVelocity;
public float gravity = 150.0f;
private bool grounded = true;
private bool jump = false;
private float currentY;
private Animator anim;
private AnimatorStateInfo currentBaseState;
private static int fallState = Animator.StringToHash("Base Layer.Fall");
private static int rollState = Animator.StringToHash("Base Layer.Roll");
private static int locoState = Animator.StringToHash("Base Layer.Run");
private static int jumpState = Animator.StringToHash("Base Layer.Jump");
private void Update()
{
currentY = verticalVelocity * Time.fixedDeltaTime;
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
grounded = true;
if (isGround() && currentY < 0f)
{
verticalVelocity = 0f;
currentY = 0f;
grounded = true;
jump = false;
fall = false;
if (currentBaseState.fullPathHash == locoState)
{
if (Input.GetButtonDown("Jump") && grounded && currentY == 0f)
{
grounded = false;
jump = true;
verticalVelocity = 0f; //I have tried here to stop gravity but don't work
follower.motion.offset = new Vector2(follower.motion.offset.x, verticalVelocity);
}
}
else if (currentBaseState.fullPathHash == jumpState)
{
Debug.Log("Jumping state");
collider.height = anim.GetFloat("ColliderHeight");
collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
else if (jump)
{
follower.motion.offset = new Vector2(follower.motion.offset.x, 0.0f); //I have tried here to stop gravity but don't work
}
else
{
grounded = false;
jump = false;
fall = true;
}
anim.SetBool("Grounded", grounded);
anim.SetBool("Jump", jump);
anim.SetBool("Fall", fall);
if (fall)
{
if (currentBaseState.fullPathHash == fallState)
{
Debug.Log("falling");
collider.height = anim.GetFloat("ColliderHeight");
collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
else
{
if (currentBaseState.fullPathHash == rollState)
{
Debug.Log("Roll");
collider.height = anim.GetFloat("ColliderHeight");
collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
MoveLeftRight();
verticalVelocity -= gravity * Time.fixedDeltaTime;
follower.motion.offset = new Vector2(follower.motion.offset.x, currentY);
Z - Forward and Backward
follower.followSpeed = speed;
}
【问题讨论】: