facebook twitter hatena line email

「Unity/3d/アニメーション」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(途中で遷移)
行63: 行63:
 
==オブジェクト接触判定==
 
==オブジェクト接触判定==
 
#PlayerのGameObjectのInspectorのTagにPlayerを追加
 
#PlayerのGameObjectのInspectorのTagにPlayerを追加
#敵オブジェクトにAddComponentでBoxColliderを追加
+
#敵オブジェクトにAddComponentでCupsuleColliderを追加
 
#的オブジェクトにAddComponentでRigidbodyを追加(Rigidbodyは境目が動いた時にOnCollisionEnterが実行されるので追加)
 
#的オブジェクトにAddComponentでRigidbodyを追加(Rigidbodyは境目が動いた時にOnCollisionEnterが実行されるので追加)
 
#csに以下のようにOnCollisionEnterを追加
 
#csに以下のようにOnCollisionEnterを追加
行71: 行71:
 
{
 
{
 
     Animator animator;
 
     Animator animator;
     BoxCollider boxCollider;
+
     CupsuleCollider collider;
 
     void Start()
 
     void Start()
 
     {
 
     {
 
         animator = GetComponent<Animator>();
 
         animator = GetComponent<Animator>();
         boxCollider = GetComponent<BoxCollider>();
+
         collider = GetComponent<CupsuleCollider>();
 
     }
 
     }
 
     private void OnCollisionEnter(Collision collision)
 
     private void OnCollisionEnter(Collision collision)

2020年6月8日 (月) 16:21時点における版

人型アセットのDL

  1. AssetStoreからZonbie ( https://assetstore.unity.com/packages/3d/characters/humanoids/zombie-30232?locale=ja-JP ) をダウンロード
  2. それをimport
  3. PrefubからZonbie1をヒエラルキーにドラッグ
  4. Assetsの下にAnimetionsのdirを作成しCreate/AnimatorControllerを作成
  5. 例として、ZonbieAnimatorControllerとして作成
  6. Zonbie1のanimetorにPrefubのAnimattionsのZonbie~を全てZonbieAnimatorController上へドラッグして入れる
  7. defaultをZ_Idolを設定
  8. parameterにfloatのSpeedを設定する
  9. Z_IdolでMakeTransitionからZ_WalkへSpeed>0.1で設定する
  10. zonbieオブジェクトに以下csをアタッチする

ZonbieController.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/

キーボード入力でアニメーションを追加

  1. 上で作った、ZonbieAnimatorControllerにZ_Attackを追加
  2. parameterにtriggerのAttackを設定する
  3. Z_IdolでMakeTransitionからZ_AttackへプロパティAttackを設定する
  4. 以下を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にチェックを入れる

アクション途中で遷移

Transition/InspectorのHas Exit Timeを外す

オブジェクト接触判定

  1. PlayerのGameObjectのInspectorのTagにPlayerを追加
  2. 敵オブジェクトにAddComponentでCupsuleColliderを追加
  3. 的オブジェクトにAddComponentでRigidbodyを追加(Rigidbodyは境目が動いた時にOnCollisionEnterが実行されるので追加)
  4. csに以下のようにOnCollisionEnterを追加
public class EnemyController : MonoBehaviour
{
    Animator animator;
    CupsuleCollider collider;
    void Start()
    {
        animator = GetComponent<Animator>();
        collider = GetComponent<CupsuleCollider>();
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            StartCoroutine(AttackTimer());
        }
    }
    IEnumerator AttackTimer()
    {
        animator.SetTrigger("Attack");
        yield return new WaitForSeconds(1);
    }
}