【ダークソウル風ゲームを作りたい(Unity)。その5】

今回はスプリントアニメーションを実装します。
youtu.be

・スプリントアニメーションを用意します。私はmixamoからダウンロードしました。
RigをHumanoidに変更します。Animationを下記のように設定します。

・スプリントアニメーションをBlend Treeに設定します。

・下記スクリプトを追加します。

public class InputHandler : MonoBehaviour //+EP1 playerにアタッチ
    {
        public float rollInputTimer; //+EP5 ローリングボタンを押下している時間を格納
        public bool sprintFlag; //+EP5 スプリントフラグがOnの場合、true

        //+EP4 ローリングをONした場合の設定
        private void HandleRollInput(float delta)
        {
            //ローリングボタン押下でb_input=true
            b_Input = inputActions.PlayerActions.Roll.phase == UnityEngine.InputSystem.InputActionPhase.Started;

            //ローリングボタンを押下中
            if (b_Input)
            {
                //+EP5
                rollInputTimer += delta;
                sprintFlag = true;
            }
            else //+EP5
            {
                //ローリングボタンを下記条件でON/OFFした場合、ローリングを行う
                if(rollInputTimer>0 && rollInputTimer < 0.5f)
                {
                    sprintFlag = false;
                    rollFlag = true;
                }

                rollInputTimer = 0;
            }
        }
   }
 public class PlayerLocomotion : MonoBehaviour //+EP1 playerにアタッチ
    {
     float sprintSpeed = 7; //+EP5 スプリントスピード

        public void Update()
        {
            //Time.deltaTime:前回のフレームからの経過時間
            float delta = Time.deltaTime;

            isSprinting = inputHandler.b_Input; //+EP5

            inputHandler.TickInput(delta);
            HandleMovement(delta);
            HandleRollingAndSprinting(delta); //+EP4   
        }

        //+EP4 プレイヤーの移動を制御
        public void HandleMovement(float delta)
        {
            //+EP5 ローリング中の場合、以降なにもしない
            if (inputHandler.rollFlag)
                return;

            moveDirection = cameraObject.forward * inputHandler.vertical;
            //cameraObject.forward:world spaceに対するプレイヤーの前方位置(0.0, 0.0, -1.0)
            //inputHandler.vertical:キーボードW(+1) or S(-1)
            //moveDirection:キーボードW(0.0, 0.0, -1.0) or S(0.0, 0.0, 1.0)

            moveDirection += cameraObject.right * inputHandler.horizontal;
            //cameraObject.right:world spaceに対するプレイヤーの右位置(-1.0, 0.0, 0.0)
            //inputHandler.horizontal:キーボードA(-1) or D(+1)
            //cameraObject.right * inputHandler.horizontal:キーボードA(1.0, 0.0, 0.0) or D(-1.0, 0.0, 0.0)
            // moveDirection:キーボードW,A押下の場合(1.0, 0.0, -1.0) 

            //正規化:ベクトルの向きは変えずに大きさを1にする
            moveDirection.Normalize();

            moveDirection.y = 0; //+EP2

            float speed = movementSpeed;

            //+EP5 スプリントフラグがONになった場合の設定
            if (inputHandler.sprintFlag)
            {
                speed = sprintSpeed;
                isSprinting = true;
                moveDirection *= speed;
            }
            else
            {
                moveDirection *= speed;
            }

            //ProjectOnPlane:斜面に沿ったベクトルを計算
            Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);

            //velocityによりプレイヤー移動
            rigidbody.velocity = projectedVelocity;

            //animatorの変数Verticalと変数Horizontalの値を設定
            animatorHandler.UpdateAnimatorValues(inputHandler.moveAmount, 0, isSprinting);

            if (animatorHandler.canRotate)
            {
                HandleRotaion(delta);
            }
        }
    }
public class AnimatorHandler : MonoBehaviour //+EP1 player直下のPaladinにアタッチ
{
  public void UpdateAnimatorValues(float verticalMovement,float horizontalMovement,bool isSprinting)
  {
       Vertical
    
      Horizontal

     //+EP5 スプリントフラグがONになった場合の設定
            if (isSprinting)
            {
                v = 2;
                h = horizontalMovement;
            }
            // anim.SetFloat(id, 値,値が変化する幅 , 1フレームの時間幅);
            anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
            anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);    
  }
}
public class PlayerManager : MonoBehaviour //+EP4 playerにアタッチ
{
        void Update()
        {
            //変数isInteractingを取得
            inputHandler.isInteracting = anim.GetBool("isInteracting");

            inputHandler.rollFlag = false;

            inputHandler.sprintFlag = false; //+EP5
        }
}

今回は以上です。