(Unity6)カメラの向きに合わせて方向計算+空中で反転移動を可能にする

(サンプルコード)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class PlayerController : MonoBehaviourPunCallbacks
{
    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;

    private Vector3 moveDirection = Vector3.zero;
    private CharacterController controller;
    private Animator animator;
    private Camera cam;

    // ★ジャンプ中かどうか
    private bool isJumping = false;
    private float verticalVelocity = 0f;

    void Start()
    {
        TryGetComponent(out controller);
        animator = GetComponent<Animator>();
        cam = Camera.main;
    }

    void Update()
    {
        if (!photonView.IsMine) return;

        // 入力取得(常に)空中でも反映させる
        var h = Input.GetAxis("Horizontal");
        var v = Input.GetAxis("Vertical");

        // カメラの向きに合わせて方向計算
        Vector3 forward = cam.transform.forward;
        forward.y = 0;
        Vector3 right = cam.transform.right;
        Vector3 inputDir = forward * v + right * h;
        inputDir = inputDir.normalized;

        // 移動アニメ(地上・空中共通)
        animator.SetFloat("MovePower", inputDir.magnitude);

        // 回転(移動入力があれば)
        if (inputDir.magnitude >= 0.1f)
        {
            transform.rotation = Quaternion.LookRotation(inputDir);
        }

        // 地上の時のジャンプ処理
        if (controller.isGrounded)
        {
            if (isJumping)
            {
                isJumping = false; // 着地したらジャンプ終了
            }

            verticalVelocity = -1f; // 地面に張り付くように微調整

            if (Input.GetKeyDown(KeyCode.Space))
            {
                verticalVelocity = jumpSpeed;
                animator.SetTrigger("Jump");
                isJumping = true;
            }
        }
        else
        {
            verticalVelocity -= gravity * Time.deltaTime;
        }

        // 最終的な移動ベクトル(横+縦)
        moveDirection = inputDir * speed;
        moveDirection.y = verticalVelocity;

        controller.Move(moveDirection * Time.deltaTime);
    }
}

(着地時の足すべり防止)


(実行確認)

・空中で反転移動ができれば成功です。