メインコンテンツまでスキップ

プレイヤーの向いている方向を基準にして動くキャラクターのサンプルプログラム

この例では、WASDキーを使用してキャラクターを移動し、マウスの向きに基づいてプレイヤーが向く方向を制御します。

using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float speed = 5f; // プレイヤーの移動速度

void Update()
{
Rotate(); // 回転を先に実行
Move(); // 移動を後に実行
}

private void Move()
{
// 入力を取得
float moveInput = Input.GetAxis("Vertical"); // W/Sキーまたは上/下スティック

// プレイヤーの向いている方向を取得
Vector3 moveDirection = transform.forward * moveInput; // プレイヤーの前方向に基づく移動

// プレイヤーの向いている方向に基づいて移動
if (moveDirection.magnitude > 0)
{
// 移動
transform.Translate(moveDirection * speed * Time.deltaTime, Space.World);
}
}

private void Rotate()
{
// マウスの位置を取得
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero); // 地面の平面
float rayDistance;

// レイと平面の交点を計算
if (groundPlane.Raycast(cameraRay, out rayDistance))
{
Vector3 pointToLook = cameraRay.GetPoint(rayDistance); // 交点を取得

// プレイヤーの向きを計算
Vector3 direction = pointToLook - transform.position; // プレイヤーの位置から交点までの方向
direction.y = 0; // Y成分を0にして水平面にする

// 向きを補間してスムーズに回転
if (direction != Vector3.zero)
{
Quaternion rotation = Quaternion.LookRotation(direction); // 向きを計算
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 10f); // スムーズに回転
}
}
}
}

Time.deltaTimeについて

Rotateメソッドについて

  • Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);:
    マウスの位置から、カメラを通して地面に向かうレイを作成します。

  • Plane groundPlane = new Plane(Vector3.up, Vector3.zero);:
    Y軸が上向きの平面を定義します。この平面は地面を表します。Z方向とX方向には限界がなく、無限に続きます。
    法線ベクトル: (0, 1, 0) — Y軸の上方向を向いています。
    オフセット : 0 — 原点(0, 0, 0)を通る平面です

  • if (groundPlane.Raycast(cameraRay, out rayDistance)):
    レイと平面が交差するかをチェックし、交差した場合はその距離を(rayDistanceで)取得します。この場合、平面側で交差を確認している

  • Vector3 pointToLook = cameraRay.GetPoint(rayDistance);: レイが平面と交わったポイントを取得します。

  • Vector3 direction = pointToLook - transform.position;: プレイヤーの位置から、向くべきポイントまでの方向を計算します。

  • direction.y = 0;:
    Y成分を0にすることで、移動方向を水平面に制限します。

  • if (direction != Vector3.zero):
    向くべき方向が存在する場合に処理を行います。

  • Quaternion rotation = Quaternion.LookRotation(direction);:
    目標とする方向を元に回転のクォータニオンを計算します。

  • transform.rotation = Quaternion.Slerp(Quaternion型 ,Quaternion型 ,float ): 現在の回転から目標の回転にスムーズに補間(球面補間)します。これにより、プレイヤーが滑らかに回転します。

    • 第1引数 (transform.rotation): 現在の回転(Quaternion)です。これはオブジェクトの現在の姿勢を示します。
    • 第2引数 (rotation):
      目指す回転(Quaternion)です。これは新しい方向に回転させたい場合の目標の姿勢を示します。
    • 第3引数 (Time.deltaTime * 10f):
      補間の割合を示します。ここでは、スピードを調整するためにTime.deltaTime(フレームごとの経過時間)に10を掛けています。この値が大きいほど、目標の回転に速く到達します。

もしVector3型を使いたい場合

Vector3 を使いたい場合は、それを一度 Quaternion に変換してから使用する必要があります。例えば、Vector3 で方向を表現している場合は、次のように Quaternion.LookRotation を使って変換できます:

Quaternion currentRotation = transform.rotation; // 現在の回転
Vector3 targetDirection = new Vector3(1, 0, 0); // 目標とする方向
Quaternion targetRotation = Quaternion.LookRotation(targetDirection); // Vector3をQuaternionに変換

// 回転のスムーズな補間
transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, Time.deltaTime * 5f);

このように、Quaternion.Slerp の引数は四元数である必要があり、Vector3 のような他の型を使いたい場合は、Quaternion に変換してから使用します。