Unity黑魂学习笔记P4 rolling

新增输入

        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);
        }
    }
}

翻滚的方向的逻辑和人物运动逻辑类似。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值