「Unity/3d/アニメーション/追跡」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→追跡されない時) |
|||
| 行37: | 行37: | ||
*対象オブジェクトのscaleを変えてみる。 | *対象オブジェクトのscaleを変えてみる。 | ||
*bakeを再度やってみる | *bakeを再度やってみる | ||
| + | |||
| + | ==正面を向いて追跡== | ||
| + | <pre> | ||
| + | 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; | ||
| + | } | ||
| + | </pre> | ||
| + | 参考:https://qiita.com/ganbaru/items/d4fe649370ce4451db80 | ||
==参考== | ==参考== | ||
2021年1月13日 (水) 05:01時点における版
追跡アクション追加
- 敵オブジェクトにAddComponentsでRigidbodyとCapsuleColliderを追加
- 床オブジェクトの(名前の横にある)staticにチェックをつける
- Window/AI/Navigationから床オブジェクトを選択し、Bakeタブを選択しBakeボタンを押す。
- 敵オブジェクトのAddComponentsからNavMeshAgentを追加
- 敵オブジェクトに以下csのようにNavMeshAgentを追加
- 障害物オブジェクトを設置してstaicにチェックを入れる。
- 障害物オブジェクトにも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
