【UnityC#講座】3D人型モデルを三人称視点で動かす操作を改良

3D人型操作改良版/トップ

Unity2019.2.2f1
Windows10

3D人型操作改良版/トップ

【UnityC#講座】3D人型モデルを三人称視点で動かす【ユニティちゃん】

以前この記事で紹介したユニティちゃんを三人称視点で動かす方法を改良しました。
Inspectorの設定なしで動かせます。
AnimatorControllerの設定は以前と同じなので上の記事を参考にしてください。

■Cameraの軸に入れるスクリプト

まずはPlayer、操作するキャラクターのタグをPlayerにします。
今回はスクリプトでタグからPlayerを取得することにしました。

3D人型操作改良版/タグ

空のオブジェクト「Axis」にAxisスクリプトを入れます。
そしてゲームを実行するとAxisはPlayerに追随し、マウスの動きで回転して向きを変えます。
以前のはInspecotrからPlayerとMain Cameraを設定しないといけませんでしたが今回はその必要はありません。
Main CameraはAxisの子オブジェクトになり、Axisに合わせて回転します。
そしてスクロールでPlayerにCameraが近づきますが、行き過ぎないように設定しました。
あとPlayerにもカメラ(の軸)が少し遅れてついていくようにしました。
InspecotrでCamera(の軸)の高さやPlayerからの距離、そしてカメラ(の軸)がついていくスピードを設定できます。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Axis : MonoBehaviour
{
//X軸の角度を制限するための変数
float angleUp = 60f;
float angleDown = -60f;
//ユニティちゃんをInspectorで入れる
GameObject player;
//Main CameraをInspectorで入れる
Camera cam;
//AxisからのCameraの距離
public float distance = 5f;
//AxisとCameraの高さ
public float height = 2f;
//Cameraのスピード、少ないほど遅く
public float attenuate = 5f;
//Cameraが回転するスピード
[SerializeField] float rotate_speed = 3;
//マウススクロールの値を入れる
[SerializeField] float scroll;
void Start()
{
//Playerをタグから取得
player = GameObject.FindGameObjectWithTag("Player");
//Main Cameraを取得
cam = Camera.main;
cam.transform.parent = transform;
//CameraのAxisに相対的な位置をlocalPositionで指定
cam.transform.localPosition = new Vector3(0, transform.position.y, -distance);
//CameraとAxisの向きを最初だけそろえる
cam.transform.localRotation = transform.rotation;
}
void Update()
{
// 本来到達しているべきカメラ位置
var camPos = player.transform.position + new Vector3(0, height, 0);
transform.position = Vector3.Lerp(transform.position, camPos, Time.deltaTime * attenuate);
//マウススクロールの値を入れる
scroll = Input.GetAxis("Mouse ScrollWheel");
//Cameraの位置、Z軸にスクロール分を加える
cam.transform.localPosition
= new Vector3(cam.transform.localPosition.x,
cam.transform.localPosition.y,
cam.transform.localPosition.z + scroll);
//カメラがスクロールでPlayerの前に行かないようにする
if(cam.transform.localPosition.z > -1)
cam.transform.localPosition = new Vector3(cam.transform.localPosition.x,
cam.transform.localPosition.y,
-1);
//Cameraの角度にマウスからとった値を入れる
transform.eulerAngles += new Vector3(
Input.GetAxis("Mouse Y") * rotate_speed,
Input.GetAxis("Mouse X") * rotate_speed
, 0);
//X軸の角度
float angleX = transform.eulerAngles.x;
//X軸の値を180度超えたら360引くことで制限しやすくする
if (angleX >= 180)
{
angleX = angleX - 360;
}
//Mathf.Clamp(値、最小値、最大値)でX軸の値を制限する
transform.eulerAngles = new Vector3(
Mathf.Clamp(angleX, angleDown, angleUp),
transform.eulerAngles.y,
transform.eulerAngles.z
);
}
}
view raw 44.cs hosted with ❤ by GitHub

スポンサーリンク

■Playerに入れるスクリプト

こちらもInspecotrから設定しなくて済むようにしました。
しかし移動スピードはInspecotrから変更できます。
あと後ろ歩きを少し遅くしました。
他は依然と同じです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//CapsuleColliderとRigidbodyを追加
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class Old : MonoBehaviour
{
//移動スピード
[SerializeField] float speed = 4f;
//Animatorを入れる
private Animator animator;
//メインカメラのTransformを入れる
Transform cam;
//Rigidbodyを入れる
Rigidbody rb;
//Capsule Colliderを入れる
CapsuleCollider caps;
void Start()
{
//Animatorコンポーネントを取得
animator = GetComponent<Animator>();
//Rigidbodyコンポーネントを取得
rb = GetComponent<Rigidbody>();
//RigidbodyのConstraintsを3つともチェック入れて
//勝手に回転しないようにする
rb.constraints = RigidbodyConstraints.FreezeRotation;
//メインカメラのTransformを取得
cam = Camera.main.transform;
//CapsuleColliderコンポーネントを取得
caps = GetComponent<CapsuleCollider>();
//CapsuleColliderの中心の位置を決める
caps.center = new Vector3(0, 0.76f, 0);
//CapsuleColliderの半径を決める
caps.radius = 0.23f;
//CapsuleColliderの高さを決める
caps.height = 1.6f;
}
void Update()
{
//A・Dキー、←→キーで横移動
float x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
//W・Sキー、↑↓キーで前後移動
float z = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed;
//AnimatorControllerのParametersに数値を送って
//アニメーションを出す
animator.SetFloat("X", x * 50);
animator.SetFloat("Y", z * 50);
//前移動の時だけ方向転換をさせる
if (z > 0)
{
transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x,
cam.eulerAngles.y, transform.rotation.z));
}else
//zが0より小さい(後退する)とスピードが落ちる
if(z < 0)
{
z = z / 1.5f;
}
//xとzの数値に基づいて移動
transform.position += transform.forward * z + transform.right * x;
}
}
view raw 44a.cs hosted with ❤ by GitHub

今回はこれで終わりです。
お疲れ様でした。

こちらに続きます。

【UnityC#講座】ユニティちゃんにパンチさせる

さらに続きはこちらです。

【UnityC#講座】ユニティちゃんに殴り合いをさせる【NavMeshAgent】

■関連記事

【UnityC#講座】上下キーで前進後退、左右キーで方向替える移動法【ユニティちゃん】

ユニティちゃんライセンス

この作品はユニティちゃんライセンス条項の元に提供されています

スポンサーリンク

目次に戻る