新增输入
private void HandleRollInput(float delta)
{
b_Input = inputActions.PlayerActions.Roll.phase == UnityEngine.InputSystem.InputActionPhase.Started;
if (b_Input)
{
rollFlag = true;
}
}
输入很简单,就是在InputHandler里面绑定好输入对应的变量即可,这里有点小问题,就是UnityEngine.InputSystem.InputActionPhase.Started;还需要设置Interactions新加一个Tap才行
**更正:**这里还是不要勾选为Tap,还是应该在代码里修改b_Input = inputActions.PlayerActions.Roll.IsPressed();这样就可以了。
动画控制
public void PlayTargetAnimation(string targetAnim, bool isInteracting)
{
anim.applyRootMotion = isInteracting;
anim.SetBool("isInteracting", isInteracting);
anim.CrossFade(targetAnim, 0.2f);
}
AnimatorHanlder新加这个函数,用来播放指定的动画,并且通过isInteracting设置是否启用根运动。如果启用根运动,我们还需要手动设置角色的位移。
private void OnAnimatorMove()
{
if(inputHandler.isInteracting == false)
{
return;
}
float delta = Time.deltaTime;
playerLocomotion.rigidbody.drag = 0;
Vector3 deltaPosition = anim.deltaPosition;
deltaPosition.y = 0;
Vector3 velocity = deltaPosition / delta;
playerLocomotion.rigidbody.velocity = velocity;
}
通过如下代码,我们可以把角色根运动和实际的rigidboy对应起来,不然会出现摄像机不跟随角色的效果。
变量的复原
我们在上面只是设置isInteracting和rollFlag,我们需要在动画状态机退出我们的Roll状态时恢复这些变量。
翻滚的方向
private void HandleRollingAndSprint(float delta)
{
if (animatorHandler.anim.GetBool("isInteracting"))
{
return;
}
if (inputHandler.rollFlag)
{
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
if(inputHandler.moveAmount > 0)
{
animatorHandler.PlayTargetAnimation("Rolling", true);
moveDirection.y = 0;
Quaternion rollRotation = Quaternion.LookRotation(moveDirection);
myTransform.rotation = rollRotation;
}
else
{
animatorHandler.PlayTargetAnimation("Backstep", true);
}
}
}
翻滚的方向的逻辑和人物运动逻辑类似。