(サンプルコード)
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;
void Start()
{
TryGetComponent(out controller);
animator = GetComponent<Animator>();
// ★カメラの向きに進む
cam = Camera.main;
}
void Update()
{
if (photonView.IsMine)
{
if (controller.isGrounded)
{
var h = Input.GetAxis("Horizontal");
var v = Input.GetAxis("Vertical");
// ★カメラの向きに進む
// カメラのベクトルを取得
Vector3 forward = cam.transform.forward;
forward.y = 0;
Vector3 right = cam.transform.right;
moveDirection = forward * v + right * h;
// ★カメラのベクトルと進行方向を一致させる。
if (moveDirection.magnitude >= 0.1f)
{
transform.rotation = Quaternion.LookRotation(moveDirection);
}
// ★アニメーション
animator.SetFloat("MovePower", new Vector3(h, 0, v).magnitude);
moveDirection = moveDirection * speed;
if (Input.GetKeyDown(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y = moveDirection.y - (gravity * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
}
}
}
(アニメーションの設定)
(実行確認)