- Home /
Patrolling AI doesn't see player if it's standing still
I have a problem with my patrolling AI script (again). I'm going to use it in my topdown Pac-Man style stealth game.
So my current enemy has a large box collider trigger component and the following script attached to it. The script moves the enemy around through waypoints. It also checks if the player enters the trigger and uses linecasting to tell if there are walls or anything blocking the view.
var waypoint : Transform[];
var speed : float = 10;
var currentWaypoint : int = 0;
var loop : boolean = true;
var player : GameObject;
private var character : CharacterController;
private var inRange : boolean = false;
function Start ()
{
character = GetComponent(CharacterController);
}
function OnTriggerStay(CollisionInfo : Collider)
{
if(CollisionInfo.gameObject.tag == "Player")
{
inRange = true;
}
}
function Update ()
{
if(inRange == true)
{
var hit : RaycastHit;
if(!Physics.Linecast(transform.position, player.transform.position,hit))
{
Debug.Log("I see you!");
}
}
if(currentWaypoint < waypoint.length)
{
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 1)
{
transform.position = target; // force character to waypoint position
currentWaypoint++;
}
else
{
transform.LookAt(target);
character.Move(moveDirection.normalized * speed * Time.deltaTime);
}
}
else
{
if(loop)
{
currentWaypoint=0;
}
}
}
function OnTriggerExit (CollisionInfo : Collider)
{
if(CollisionInfo.gameObject.tag == "Player")
{
inRange = false;
}
}
This works very fine, but if I move my player into the enemy's patrolling route and stand still when it goes by, it doesn't notice me at all. But it does if it walks straight at me (so that our CharacterControllers collide).
Why is this happening? And how could I fix this? All answers and suggestions are welcome. I have no idea!
Thanks in advance!
How are you moving the player? Is it a Character Controller?
Answer by sneftel · Jul 05, 2011 at 04:44 PM
Give your enemy a RigidBody and set it to kinematic. Otherwise its own motion won't invoke triggers.
Oh, I just managed to make my script use CharacterController.$$anonymous$$ove to move with ins$$anonymous$$d of Rigidbody.velocity yesterday. It was very glitchy that way. And making it kinematic would require many lines of script to prevent it from going through walls, wouldn't it? Any other ways to do this?
None of what you said there is relevant. Having a kinematic RigidBody does not affect character control.
Character Controllers are detected by triggers and colliders. You may have problems if your player is moved with Translate. Are you using Character.$$anonymous$$ove to move your player too?
Now I see what Ben meant! So the enemy can have BOTH Rigidbody AND a Character Controller. Sorry for that... Now it detects me even if I'm standing still, and works good. Thank you! And yes I use Character.$$anonymous$$ove in my player's script aswell.
Read his question again -- in addition to a character controller, he also has a large box collider attached. That's the volume he wants to use for triggering.