【移動判定】Input.GetAxis("Horizontal")とInput.GetAxis("Vertical"))を使った時の処理

プレイヤーが動いているときだけ何かをしたいときがあると思います。

今回プレイヤーはキャラクターコントローラで動かしている前提でお話していきます。地面がついているときに移動するプログラムです。

移動時の入力処理変数moveDirectionにmagnitudeクラスで移動の距離を出す。指定した値0.1より大きければ移動したと判定する処理です。

if(moveDirection.magnitude > 0.1f){

//処理内容

//今回はカメラの揺れの処理を記述している

}

if(controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if(moveDirection.magnitude > 0.1f){
                Vector3 handBob_ = headBob_.DoHeadBob(1.0f);
                FPSCamera.transform.localPosition = handBob_;

            }