- Home /
NPC AI Unresponsive after spotting player once
Hi there,
I am working on an enemy AI for my game. So far I managed to get most of it working. The enemy patrols to certain waypoints which is perfectly fine. In one of my scripts I have a variable called playerInSight. This variable turns to true whenever the player enters a collider which is on one of the child objects of the enemy. I just made a cover system where it finds the nearest possible cover and moves towards that position. If I tick the variable related to that process in the inspector the enemy goes into cover perfectly (which is the playerAware variable). However, if the playerInSight boolean is set to true once in the scene, the enemy becomes unresponsive and doesn't continue with his patrol and he is also not able to get into cover anymore (since he is unresponsive to anything except for the playerInSight variable). Because of this, I think the problem lays in the script where that variable is created. This is the script, its called EnemySight:
#pragma strict
var playerInSight : boolean = false;
var playerAware : boolean = false;
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player")
{
playerInSight = true;
playerAware = true;
}
}
function OnTriggerExit(other1 : Collider)
{
if(other1.tag == "Player")
{
playerInSight = false;
}
}
Another possibility is that there is something wrong in the scripts which handles the shooting and rotating towards the player of the enemy. I'll post this script here aswell:
#pragma strict
var enemySight : EnemySight;
var readyFire : boolean = false;
var rotationSpeed : float = 5.0;
private var anim : Animator;
private var player : GameObject;
private var playerStats : PlayerStats;
function Start ()
{
anim = GetComponent(Animator);
player = GameObject.Find("Player");
playerStats = player.GetComponent(PlayerStats);
enemySight = GetComponentInChildren(EnemySight);
}
function Update()
{
if(enemySight.playerInSight)
{
var direction : Vector3 = player.transform.position - transform.position;
var rotation = Quaternion.LookRotation(direction);
rotation.z = 0.0;
rotation.x = 0.0;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
readyFire = true;
}
else
{
readyFire = false;
}
if(readyFire)
{
anim.SetBool("Shooting", true);
playerStats.playerHealth -= 50 * Time.deltaTime;
}
else
{
anim.SetBool("Shooting", false);
}
}
I feel like i've tried everything to fix this issue without results. Is anyone willing to give me a hand with this? If I need to post some of my other scripts which work together with these two to actually make the AI, i'll post them here aswell.
Answer by Xetros · Aug 31, 2015 at 12:21 PM
For people who stumble upon the same problem, managed to fix it by restarting the NavMeshAgent with the Resume() function.
Answer by Rob Howard · Aug 31, 2015 at 12:21 PM
@Xetros So I'm pretty new to Unity, but I'll take a stab at it. I think it has something to do with all the AI behavior being placed inside Update. Update being a big loop, it's probably messing with everything.
I'm just guessing, but I think you might want to use a custom event to trigger the AI behavior, so when the player hits the trigger, an event fires. The enemy AI has an event listener on it that modifies it's behavior, in essence, calling your code you wrote under Update.
Your answer
Follow this Question
Related Questions
Is the Player Being Looked at 1 Answer
EnemyAI C# Error 1 Answer
How to make an AI like slender ? 1 Answer
AI Following Problem 1 Answer
sprite changes z position without any input to do so 1 Answer