- Home /
Enemy not detecting player
Hello, I'm making a turret AI and have attached a trigger to it. Once the player enters the trigger the turret should look at it. In order to do that I created a bool variable which should become false once the player enters the trigger but it's not working. Here's my script for the turret:
using UnityEngine;
using System.Collections;
public class TurretAI : MonoBehaviour {
public bool idleState = TurretSight.idleState;
public float speed = 10;
public Transform player;
public float degree;
// Use this for initialization
void Start () {
idleState = true;
}
// Update is called once per frame
void Update () {
if(idleState){
transform.Rotate(Vector3.up * Time.deltaTime * speed);
degree += Time.deltaTime;
if(degree >= 2){
speed *= -1;
degree = -2;
}
}
else
transform.LookAt(player);
}
}
And here's the turret's sight (PS: it's a custom pyramid collider from another gameobject)
using UnityEngine;
using System.Collections;
public class TurretSight : MonoBehaviour {
public static bool idleState;
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay(Collider enter){
if(enter.gameObject == player){
idleState = false;
}
}
void OnTriggerExit(Collider exit){
if(exit.gameObject == player)
idleState = false;
}
}
Any help will be greatly appreciated. Thanks in advance.
Enemy not detecting player
player apparently is a ninja.
Could be lots of things. Collision layers set up? Player and turret both have colliders? Turret has the player variable set to the actual player? Player is ninja?
layers are set up, they definitely both have colliders. The ontriggerstay works i added a debug.log that gave me the correct message. So yeah I'm gonna go with the player being a tenchu level ninja...
Oh!. Do you have the colliders set as triggers? And I think that means you player will have to have a rigidbody attached as well.
Answer by Potentii · Aug 09, 2014 at 06:17 PM
try to put a rigidBody in one of the gameObjects.
I already have a rigidbody set on the parent gameobject
hmm, the rigidBody also have the "continuous" or "continuous dynamic" collision detection checked?
Your answer
Follow this Question
Related Questions
How can I acces the script of an object that I triggered? 1 Answer
Damaging Enemies 1 Answer
Turrent detection issue. 0 Answers
Glitchy Turret Script 2 Answers
Enemy Attraction/Aggro 1 Answer