Question by
Sydneynumber9 · Aug 27, 2021 at 07:34 AM ·
c#3dvr
how to start the Enemy Animator if player is in range
Hello Together, hope someone can help.
My Enemy is always in animation loop. But i want to start the animation if the player is range, but i dont know how to do it. That my code:
namespace S3
{
public class EnemyController : MonoBehaviour
{
public AudioClip deathAudio;
public Transform target;
private UnityEngine.AI.NavMeshAgent agent;
private Rigidbody[] rbs;
void Update()
{
agent = GetComponent<NavMeshAgent>();
rbs = GetComponentsInChildren<Rigidbody>();
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
if (IsPlayerInRange(transform.position))
agent.SetDestination(target.position);
GetComponent<Animator>().speed = Random.Range(minSpeed, maxSpeed);
}
bool IsPlayerInRange(Vector3 center)
{
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
foreach (var hitCollider in hitColliders)
{
if (hitCollider.gameObject.CompareTag(target.tag))
{
if (Vector3.Distance(target.position, transform.position) < 1.5f)
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
return true;
}
}
return false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Get Direction From Raycast 0 Answers
OVR Walking Animation trigger. 0 Answers
Unity multiplayer VR Node JS 3D 0 Answers
Making KeyCodes Equal to Touch Triggers? 1 Answer
Error!!! NullReferenceException: Object reference not set to an instance of an object 2 Answers