I’m trying to do away with the giant sphere collider on my NPC that detects whether or not you are close. When you get too close, they will run away from you. When they are far enough away or you are, they go back to walking normally. The reason I want to use another method is because the sphere collider trigger is interfering with a raycast on another part I’m working on.
Here’s what I had set up before:
void OnTriggerEnter(Collider other)
{
//check if player zombie is within triggering range of character
if (other.gameObject.tag == "PlayerZombie")
{
anim.SetBool("isScared", true); //if player is too close, set character to "scared" state
print("Zombie in range!"); //print statement for test
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "PlayerZombie")
{
anim.SetBool("isScared", false); //make character not scared when player zombie is out of range
//agent.ResetPath();
print("Zombie out of range"); //test print
}
}
Here’s what I was trying to do now:
//check the surrounding area for the player zombie
void CheckForPlayer()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, 20f, out hit, 20f))
{
if (hit.collider.gameObject.tag == "PlayerZombie")
{
anim.SetBool("isScared", true); //if player is too close, set character to "scared" state
print("Zombie in range!"); //print statement for test
}
else
{
anim.SetBool("isScared", false);
print("Zombie out of range");
}
}
}
But the above is doing nothing at all. What am I doing wrong?