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

資料
- Unity】Input Systemの使い方入門(2023年度版)
- < a href="https://blog.logrocket.com/building-third-person-controller-unity-new-input-system/">
Building a third-person controller in Unity with the new input system
-
tp
MacBookAir (M1, 2020)
Tip: Apple M1
Memory: 16GB
macOS: Ventura 13.5.1
新しいプロジェクトを開始する
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 であることがわかる。/li>
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)
これ以降は、「Unity5: 自分で作成した人型モデルを利用する(移動)」
にしたがって手順を進める。
Explorer で 「MakeHuman で Unity5 で使用する人型モデルを作成する」 で作成した AsianBoy.fbx と textures/ があるフォルダを開く。
そこから、UnityのProjectウィンドウの Assets/Models/ に AsianBoy.fbx と textures/ フォルダをドラッグしてインポートする。
警告が表示された場合は、"Fix now" をクリックする。


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 とした。

Package Manager で、"Cinematic" と "Input System" という2つのパッケージがインストールされていることを確認する。

入力を Action にbindする(対応づける)ために、Input System を設定する。
- Input Action アセットを作成する。
Project ウィンドウ → 右グリック → Create $rarr; Input Actions
- 名前を Controls.inputactions に変更する。

- Project ウィンドウ内の Controls.inputactions をダブルクリックして "Controls (Input Actions)" ウィンドウを表示する。

- "Controls (Input Actions) ウィンドウの左上隅の "No Control Scheme" (または "All Control Scheme" の場合あり)を右クリックして "Add Control Scheme ..." を選択する。

- 名前を "Keyboard and Mouse" に変更し、デバイスに Keyboard と Mouse を追加して "Save" する。





- Controls ウィンドウで、"Keyboard and Mouse" スキームを選択した上で、"Action Maps" 欄に"+"して名前を Playerに変更する。
- Playersが選択された状態で、"Actions" 欄にデフォルトで "New Action" が表示されているはずである。この名前を "Jump" に変更する。
- Actions 欄の "Jump" を展開すると "" が表示されるので、それを選択して右欄の "Binding Properties" 欄の Path の次のドロップダウンアイコンをクリックする。"Keyboard" の "By Character Mapped to Key" から SPACE を選択する。





- Controls の "Actions" 欄に "Move" という名前のActionを追加する。Actions欄で"Move"が選択された状態で、右の "Action Properties"欄で設定する。
Action Type: Value
Control Type: Vector 2 ← Vector3においてz=0なのがVector2
- "Move" Action の "+" を選択して、 新しい "Up/Down/Left/Right Composite" バインディングを追加する。
Up/Down/Left/Right のそれぞれに、W,S,A,D キーー割り当てる。
それぞれの "Use in cnotrol scheme" において
"Keyboard and Mouse" がチェックする必要がある。


- Controls の "Actions" 欄に "Look" Action を追加する。
Action Type: Value
Contgrol Type: Delta

- "Look" Action の Binding では、Binding Properties 欄において "Keyboard and Mouse" をチェックした上で、
Path でMouseのDeltaをバインドする。


- Controls ウィンドウで "Save Asset" ボタンをクリックする。

- "Save Asset" ボタンをクリックすると、
Assets フォルダの Controls.inputactions が
Prefab 化して、中に Controls.cs が含まれるようになる。
「Assetsフォルダの Controls.inputactions の隣に Controls.cs ファイルが生成される」としている資料があるが、
バージョンによって振る舞いに違いがあるようだ。


- Project ウィンドウで Controls.inputactions を選択している状態で、Inspector ウィンドウで、"Generate C# Class# をチェックして、
"Edit asset" の "Apply" ボタンをクリックすると Assets/Controls.cs が生成される。
C# Class File: Assets/Controls.cs
C# Class Name: Controls
C# Class Namespace <Global namespace>
- Controls.cs の中で、IPLayerActions インターフェイスが次のように定義されている。
public partial class @Controls : IInputActionCollection2, IDisposable
{
...(略)
public interface IPlayerActions
{
void OnJump(InputAction.CallbackContext context);
void OnMove(InputAction.CallbackContext context);
void OnLook(InputAction.CallbackContext context);
}
}
InputReader クラスを作成する。
- InputReader クラスを定義する C# script ファイル InputReader.cs を生成する。
- InputReader クラスは Controls.IPlayerActions インターフェイスを実装する。
- Controls.inputactions でアクションとして "Look","Move","Jump" を定義したので、
OnLook, OnMOve, OnJump メソッドが定義されている。
- Unityの新しい Input System は event-based に変更された。
InputReader.cs |
授業で配布するプリントを参照して下さい。
|
Player を設定する。
- Hierarchy の中の AsianBoy を Player に名前を変更する。
- Player に InputReader.cs を Component として Add する。

シーンを Assets/Scenes/Scene01.unity として保存する。
Top Menu Bar → File → Save as ... → Scene01
"Run" すると、W/S/A/Dキー入力に応じて Player の InputReader コンポーネントの "Move Composite" の値が変化する。
Move Composite
W: X: 0 Y: 1
S: X: 0 Y:-1
A: X:-1 Y: 0
D: X: 1 Y: 0
人型キャラクタを移動させるスクリプトを作成する。
- Projectウィンドウの Assets で右クリック &arr; 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"ボタンをクリックして、実行してみる。
/Users/nitta/Documents/unity/2021.3.16f1/Humanoid/Assets/TSP/InputSystem/StarterAssetsInputs.cs/Users/nitta/Documents/unity/2021.3.16f1/Humanoid/Assets/TSP/InputSystem/StarterAssetsInputs.cs
動作するプロジェクトのzipファイル。
Unity プロジェクトの作成環境:
PC: MacBookAir (M1, 2020)
macOS: Ventura 13.5.1
Unity Hub: 3.5.1
Unity: 2021.1.0f1 (Intel)