gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)
using UnityEngine; using UnityEngine.AI; public class EnemyAI : MonoBehaviour { public…
- User
using UnityEngine; using UnityEngine.AI;
public class EnemyAI : MonoBehaviour { public Camera camera; public float wanderRadius = 10f; public float detectionRadius = 5f; public float chaseDuration = 15f; public float stopChaseDelay = 5f; public float speed = 2f;
private GameObject player; private NavMeshAgent agent; private Animator animator; private float originalSpeed; private bool isChasing = false; private bool canChase = true; private float chaseTimer = 0f; private float stopChaseTimer = 0f; void Start() { player = GameObject.FindGameObjectWithTag("Player"); agent = GetComponent<NavMeshAgent>(); animator = GetComponent<Animator>(); originalSpeed = agent.speed; agent.speed = 5; InvokeRepeating("Wander", 5f, 15f); } void Update() { float distanceToPlayer = Vector3.Distance(transform.position, player.transform.position); if (distanceToPlayer <= detectionRadius) { if (!isChasing && canChase) { isChasing = true; agent.speed = speed; animator.SetInteger("State", 2); chaseTimer = 0f; } if (isChasing) { chaseTimer += Time.deltaTime; if (chaseTimer >= chaseDuration) { isChasing = false; agent.speed = 5; animator.SetInteger("State", 0); stopChaseTimer = 0f; canChase = false; Invoke("ResetChaseState", stopChaseDelay); } else { agent.SetDestination(player.transform.position); } } } else if (!isChasing) { stopChaseTimer += Time.deltaTime; if (stopChaseTimer >= 5f) { agent.speed = 5; animator.SetInteger("State", 0); canChase = true; } } } void Wander() { if (isChasing) return; Vector3 randomPoint = transform.position + Random.insideUnitSphere * wanderRadius; NavMeshHit hit; NavMesh.SamplePosition(randomPoint, out hit, wanderRadius, 1); Vector3 finalPosition = hit.position; agent.SetDestination(finalPosition); agent.speed = speed; animator.SetInteger("State", 1); } void ResetChaseState() { canChase = true; }}
добавь чтобы при начале преследования проигрывался выбранный звук
- Assistant
Для этого вам понадобится использовать компонент AudioSource и метод Play() для воспроизведения звука. Ниже приведен пример того, как это можно сделать в коде:
using UnityEngine; using UnityEngine.AI; public class EnemyAI : MonoBehaviour { public Camera camera; public float wanderRadius = 10f; public float detectionRadius = 5f; public float chaseDuration = 15f; public float stopChaseDelay = 5f; public float speed = 2f; public AudioClip chaseSound; private GameObject player; private NavMeshAgent agent; private Animator animator; private float originalSpeed; private AudioSource audioSource; private bool isChasing = false; private bool canChase = true; private float chaseTimer = 0f; private float stopChaseTimer = 0f; void Start() { player = GameObject.FindGameObjectWithTag("Player"); agent = GetComponent<NavMeshAgent>(); animator = GetComponent<Animator>(); audioSource = GetComponent<AudioSource>(); originalSpeed = agent.speed; agent.speed = 5; InvokeRepeating("Wander", 5f, 15f); } void Update() { float distanceToPlayer = Vector3.Distance(transform.position, player.transform.position); if (distanceToPlayer <= detectionRadius) { if (!isChasing && canChase) { isChasing = true; agent.speed = speed; animator.SetInteger("State", 2); chaseTimer = 0f; if (chaseSound != null) { audioSource.clip = chaseSound; audioSource.Play(); } } if (isChasing) { chaseTimer += Time.deltaTime; if (chaseTimer >= chaseDuration) { isChasing = false; agent.speed = 5; animator.SetInteger("State", 0); stopChaseTimer = 0f; canChase = false; Invoke("ResetChaseState", stopChaseDelay); } else { agent.SetDestination(player.transform.position); } } } else if (!isChasing) { stopChaseTimer += Time.deltaTime; if (stopChaseTimer >= 5f) { agent.speed = 5; animator.SetInteger("State", 0); canChase = true; } } } void Wander() { if (isChasing) return; Vector3 randomPoint = transform.position + Random.insideUnitSphere * wanderRadius; NavMeshHit hit; NavMesh.SamplePosition(randomPoint, out hit, wanderRadius, 1); Vector3 finalPosition = hit.position; agent.SetDestination(finalPosition); agent.speed = speed; animator.SetInteger("State", 1); } void ResetChaseState() { canChase = true; } }В данном случае, звук будет проигрываться при начале преследования и только если переменная
chaseSoundбыла задана в инспекторе через редактор Unity.