facebook twitter hatena line email

Unity/3d/アニメーション/追跡

提供: 初心者エンジニアの簡易メモ
2022年8月19日 (金) 10:29時点におけるAdmin (トーク | 投稿記録)による版 (NavMeshAgent同士の接触半径)

移動: 案内検索

追跡アクション追加

  1. 敵オブジェクトにAddComponentsでRigidbodyとCapsuleColliderを追加
  2. 床オブジェクトの(名前の横にある)staticにチェックをつける
  3. Unityメインメニュー/Window/AI/Navigationから床オブジェクトを選択し、Bakeタブを選択しBakeボタンを押す。
  4. 敵オブジェクトのAddComponentsからNavMeshAgentを追加
  5. 敵オブジェクトに以下csのようにNavMeshAgentを追加
  6. 障害物オブジェクトを設置してstaicにチェックを入れる。
  7. 障害物オブジェクトにもBakeを設定する。
using UnityEngine.AI;
public class EnemyController : MonoBehaviour
{
    Animator animator;
    NavMeshAgent agent;
    void Start()
    {
        animator = GetComponent<Animator>();
        if (GetComponent<NavMeshAgent>() != null)
        {
            agent = GetComponent<NavMeshAgent>();
            agent.updateRotation = false;
            agent.updatePosition = true;
        }
    }
    void Update()
    {
        GameObject target = GameObject.Find("Main Camera");
        if (target != null && agent != null)
        {
            agent.SetDestination(target.transform.position);
        }
    }
}

追跡されない時

  • 対象オブジェクトのscaleを変えてみる。
  • bakeを再度やってみる

正面を向いて追跡

public Transform target;
public float speed = 0.1f;
void Start () {
    target = GameObject.Find("Target").transform;
}
void Update() {
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), 0.3f);
        transform.position += transform.forward * speed;
}

参考:https://qiita.com/ganbaru/items/d4fe649370ce4451db80

接触判定させる場合で、追いついて回転して、接触判定しないときはRigidbodyが足りないのかも・・

NavMeshAgent同士の接触

NavMeshAgent同士の接触半径を狭める場合は、InspectorのNavMeshAgentのRadiusを0.5から小さくする

参考

https://unity.moon-bear.com/3d%E3%83%9B%E3%83%A9%E3%83%BC%E3%82%B2%E3%83%BC%E3%83%A0%E3%80%8C%E3%82%B9%E3%82%B1%E3%82%A2%E3%82%AF%E3%83%AD%E3%82%A6%EF%BC%9A%E5%BB%83%E6%9D%91%E3%81%AE%E6%81%90%E6%80%96%E3%80%8D/navmesh%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%80%8C%E8%BF%BD%E3%81%84%E3%81%8B%E3%81%91%E3%81%A6%E3%81%8F%E3%82%8B%E6%95%B5%E3%82%AD%E3%83%A3%E3%83%A9%E3%80%8D%E3%82%92%E4%BD%9C%E3%82%8B/

https://qiita.com/aimy-07/items/d1fea617ab9cbb3bd1ed