2023/09/02 Updated by 
Unity
Humanoid を動かす (1) 移動
[Up]

MacBookAir (M1, 2020)
Tip: Apple M1
Memory: 16GB
macOS: Ventura 13.5.1
新しいプロジェクトを開始する
"Third Person (Core)" を選択すると、初回は "Download" ボタンが表示される。
"Download" が終了するとプロジェクトの "Create" が可能になる。
Unity Hub: 3.5.1
Unity version: 2021.3.16f1 APPLE SILICON LTS
Project Type: Third Person (Core)
Project Name: Humanoid


Unity が起動する。"Run" すると、物体の Material は正しく Render されているし、キー入力に対しても正しく動作しているように見える。


このプロジェクトで使われている Render Pipeline を確認しておく。→ UniversalRenderPipelineAsset であることがわかる。
Top Menu Bar → Edit → Project Settings → Graphics → Scriptable Render Pipeline Settings

Assets の下に TSP というフォルダを作成し、Assets の直下にあったファイルをすべて TSP の下に移動しておく。
新しいシーンを Basic (URP) で作成する。
Top Menu Bar → File → New Scene → Standard → Basic (URP)
Finder (macOS の場合、Windows では Explorer を使う) で
「MakeHuman で Unity5 で使用する人型モデルを作成する」
で作成した AsianBoy.fbx と textures/ があるフォルダを開く。
そこから、UnityのProjectウィンドウの Assets/Models/ に AsianBoy.fbx と textures/ フォルダをドラッグしてインポートする。
警告が表示された場合は、"Fix now" をクリックする。
makehuman2.zip


ProjectウィンドウでAsianBoyを選択をして、 Inspectorウィンドウで "Rig" で Animation Type を "Humanoid" に変更し、 Avatar Definition を "Create From This Model" のままとして、 "Apply"ボタンをクリックします。

Inspectorウィンドウで "configure" をクリックして、ボーンが正しく設定されたか確かめておいた方が安全です

Inspectorウィンドウで "Mapping" をクリックして、ボーンの対応を見ます。表示はSceneウィンドウの方がGameウィンドウよりも見やすいかもしれません。

Inspectorウィンドウで "Muscles & Settings" をクリックして、スライダを動かして各ボーンが正しく動作するかを確認します。確認できたら "Done" をクリックします。

Hierarchyに地面となる Planeを配置します。InspectorのTransformの からResetを選んで、Position (x,y,z)=(0,0,0)にします。
GameObject → 3D Object → Plane

Hierarchy に Assets/Models/AsianBoy を配置する。
- Assets/Models から AsianBoy を Hierarchy にドラッグする。

- InspectorのTransformのから Resetを選択して、Position (x,y,z)=(0,0,0)とする。
- InspectorでAdd Componenct をクリックして、Physics/CharacterController コンポーネントを追加する。

- AsiznBoy の Inspectorに追加された CharacterController コンポーネントの Center と Height の値を変更する。
Sceneウィンドウを表示している状態で、Hierarchy 内で AsianBoy を選択すると、
CharacterController の Capsel Collider が緑色の実線で表示される。
人型キャラクタと合致するように Center と Height の値を変更する。
ここでは Center (x,y,z)=(0,0.87,0), Height = 1.7 とした。

ユーザからの入力を扱う
Unity では「従来からの Input System」と「2020 以降に導入された新しい Input System」の2種類の
入力方式が利用可能であり、これらはプロジェクトの設定で切り替えることができる。
どちらの System にも長所と短所があるが、以下では簡単のため「従来のInput System」を使う例を説明する。
- Package Manager で、"Cinematic" と "Input System" という2つのパッケージがインストールされていることを確認する。

- プロジェクトの設定で、2種類の Input System をどちらも利用可能 (
Both
) に設定する。設定を変更後は、Unity Editor の再起動が必要になる。
Top Menu Bar → Edit → Project Settings ... → Player → Other Settings → Configuration → Active Input Handling → Both

人型キャラクタを移動させるスクリプトを作成する。
- Projectウィンドウの Assets で右クリック → Create → Folder → Scripts として フォルダを生成し、Scriptsにrenameする。

- ProjectウィンドウのAssets/Scriptsで右クリック -> Create -> C# Script として C#のファイルを生成し、PlayerMoveにrenameする。

- ProjectウィンドウのAssets/Scripts/PlayerMoveを Hierarchy の AsianBoyの上へドラッグする。Hierarchy のAsianBoyを選択すると、Inspectorウィンドウに "Player Move (Script)" コンポーネントが追加されたことがわかる。

ProjectウィンドウのAssets/Scriptsの中の PlayerMove.cs を次のように変更する。
PlayerMove.cs |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float velocity = 1.3f;
private CharacterController charController;
void Start()
{
charController = gameObject.GetComponent<CharacterController>();
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(h, 0, v);
charController.Move(velocity * Time.deltaTime * moveDirection);
}
}
|
Hierarchy の中の Main Camera の位置を変更する。
Plane はxz平面上の平面で、原点を中心として 10x10 の大きさである。
また、AsianBoyは原点にいる。
Main Camera が少し離れ過ぎているので Plane の端である Transform Position (x,y,z)=(0,1,-5)に設定する。

"Run"ボタンをクリックして、実行してみる。
「矢印キー (↑↓←→)」または「w/s/a/d キー」で AsianBoy が前後左右に動作する。地面からはみ出しても落下せずに、そのまま平行移動することがわかる。

シーンを Assets/Scenes/Scene01.unity として保存する。
Top Menu Bar → File → Save as ... → Scene01
ProjectウィンドウのAssets/Scripts に PlayerMove2.cs を生成して、内容は PlayerMove.cs から以下の変更を加えたものとする。
PlayerMove2.cs |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove2 : MonoBehaviour
{
public float velocity = 1.3f;
private CharacterController charController;
void Start () {
charController = gameObject.GetComponent<CharacterController>();
}
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(h, 0, v);
moveDirection.y += Physics.gravity.y;
charController.Move(velocity * Time.deltaTime * moveDirection);
}
}
|
Hierarchy ウィンドウの AsianBoy に Add Component する。PlayerMove.cs は Disable しておく。

実行すると、AsianBody地面からはみ出すと落下するようになる。
Assets/Scens/ にシーンを Scene02 として保存しておく。
作成したプロジェクトのzipファイル。
Unity プロジェクトの作成環境:
PC: MacBookAir (M1, 2020)
macOS: Ventura 13.5.1
Unity Hub: 3.5.1
Unity: 2021.3.16f1 (Intel)