「Unity/3d/アニメーション/追跡」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==追跡アクション追加== #敵オブジェクトにAddComponentsでRigidbodyとCapsuleColliderを追加 #床オブジェクトにNavMeshを追加するために、...」) |
(→追跡アクション追加) |
||
| 行22: | 行22: | ||
void Update() | void Update() | ||
{ | { | ||
| − | + | GameObject target = GameObject.Find("Main Camera"); | |
if (target != null && agent != null) | if (target != null && agent != null) | ||
{ | { | ||
| − | agent.SetDestination(target.position); | + | agent.SetDestination(target.transform.position); |
} | } | ||
} | } | ||
2020年6月9日 (火) 01:57時点における版
追跡アクション追加
- 敵オブジェクトにAddComponentsでRigidbodyとCapsuleColliderを追加
- 床オブジェクトにNavMeshを追加するために、下の項目の手順を行う。
- 敵オブジェクトのAddComponentsからNavMeshAgentを追加
- 敵オブジェクトに以下csのようにNavMeshAgentを追加
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);
}
}
}
- 床オブジェクトのstaticにチェックをつける
- Window/Navigationから床オブジェクトを選択し、Bakeタブを選択しBakeボタンを押す。
