「Unity/3d/アニメーション/基本」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→モーションブレンド) |
(→モーションブレンド) |
||
| 行106: | 行106: | ||
==モーションブレンド== | ==モーションブレンド== | ||
#AnimatorControllerで、モーションを右クリックしてnew BlendTreeで新規作成 | #AnimatorControllerで、モーションを右クリックしてnew BlendTreeで新規作成 | ||
| − | # | + | #数値に同期する、Parameterに項目名を設定して、 |
#Automateをオフにして、Thresholdに同期の数値を入れる。 | #Automateをオフにして、Thresholdに同期の数値を入れる。 | ||
| + | |||
| + | ===動かない場合=== | ||
| + | Parameterには、"Speed"を入れてある。以下のようにParametersに設定してある項目を入れておけばよい。 | ||
| + | animator.SetFloat("Speed", speed); | ||
2023年4月20日 (木) 18:23時点における最新版
目次
人型アセット動作
- AssetStoreからZombie ( https://assetstore.unity.com/packages/3d/characters/humanoids/zombie-30232?locale=ja-JP ) をダウンロード
- それをimport
- PrefabからZombie1をヒエラルキーにドラッグ
- Assetsの下にAnimetionsのdirを作成しCreate/AnimatorControllerを作成
- 例として、ZombieAnimatorControllerとして作成
- Zombie1のanimetorにPrefabのAnimattionsのZombie~を全てZombieAnimatorController上へドラッグして入れる
- defaultをZ_Idolを設定
- parameterにfloatのSpeedを設定する
- Z_IdolでMakeTransitionからZ_WalkへSpeed>0.1で設定する
- Zombieオブジェクトに以下csをアタッチする
ZombieController.cs
using UnityEngine.AI;
[RequireComponent(typeof(Animator))]
public class EnemyController : MonoBehaviour
{
Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
animator.SetFloat("Speed", 0.2f, 0.1f, Time.deltaTime);
}
}
参考:https://unity.moon-bear.com/zombie-slayer/enemy-animation/
キーボード入力でアニメーションを追加
- 上で作った、ZonbieAnimatorControllerにZ_Attackを追加
- parameterにtriggerのAttackを設定する
- Z_IdolでMakeTransitionからZ_AttackへプロパティAttackを設定する
- 以下をcsへ追加
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
animator.SetTrigger("Attack");
}
}
スペースを押した時に攻撃モーションが表示される。
他キーボード
if (Input.GetKey(KeyCode.UpArrow)) {
} else if (Input.GetKey(KeyCode.UpArrow)) {
} else if (Input.GetKey(KeyCode.LeftArrow)) {
} else if (Input.GetKey(KeyCode.RightArrow)) {
オブジェクトの動作を全体座標に適用
Apply Root Motionにチェックを入れる コード上だと以下のように
animator.applyRootMotion = true;
アクション途中で遷移
Transition/InspectorのHas Exit Timeを外す
オブジェクト接触判定
- PlayerのGameObjectのInspectorのTagにPlayerを追加
- 敵オブジェクトにAddComponentでCapsuleColliderを追加
- 敵オブジェクトにAddComponentでRigidbodyを追加
- csに以下のようにOnCollisionEnterを追加
public class EnemyController : MonoBehaviour
{
Animator animator;
CapsuleCollider collider;
void Start()
{
animator = GetComponent<Animator>();
collider = GetComponent<CapsuleCollider>();
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
StartCoroutine(AttackTimer());
}
}
IEnumerator AttackTimer()
{
animator.SetTrigger("Attack");
yield return new WaitForSeconds(1);
}
}
- OnCollisionEnterは衝突時に実行される(オブジェクトが停止すると以降、実行されない)
- OnCollisionExit は他のオブジェクトと衝突終了時に実行される
- OnCollisionStay は他のオブジェクトと衝突中に実行される
すり抜け
isTriggerをonにすると、衝突判定はするが、すり抜けるようになる。
デフォルトアクション
entryを右クリックでdefaultを選択し、デフォルト設定したい、アクションへドラッグ
モーションブレンド
- AnimatorControllerで、モーションを右クリックしてnew BlendTreeで新規作成
- 数値に同期する、Parameterに項目名を設定して、
- Automateをオフにして、Thresholdに同期の数値を入れる。
動かない場合
Parameterには、"Speed"を入れてある。以下のようにParametersに設定してある項目を入れておけばよい。
animator.SetFloat("Speed", speed);
