- Home /
OnTriggerEnter not working for NavMesh
I am trying to create a horror game with two creatures and 1 first person player. Like many of the horror games I have a flashlight which works well with my "cone" collider that I made in blender, I just added the shape as a child to my player and nothing else. What I'm trying to achieve is when I shine my flashlight on a creature, they start chasing after me. My creature has a NavMesh already and if my flashlight doesn't shine on it, then it just creates random waypoints in a certain range with a timer, once that timer goes off he will create a different waypoint. My "cone" object has a collider on it (and it's mesh renderer off) with the collider set to a trigger (and the flashlight and creature both have a rigid body on them so that the creature can detect collisions). The flashlight has this script on it so it can change the cone's tag from FlashlightOn to FlashlightOff using the cone as a variable. The creature is only supposed to react with the cone object when it has a tag of FlashlightOn. But, when I start my game the creature does't detect my cone and just keeps wandering with waypoints. I have no errors on my scripts either!
Creature script:
using UnityEngine;
using System.Collections;
public class CreatureAI : MonoBehaviour {
public float wanderRadius;
public float wanderTimer;
public Transform target;
private NavMeshAgent agent;
private float timer;
public bool chasing = false;
public Vector3 myTransform;
// Use this for initialization
void OnEnable () {
agent = GetComponent<NavMeshAgent> ();
timer = wanderTimer;
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if(chasing == false){
if (timer >= wanderTimer) {
Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
agent.SetDestination(newPos);
timer = 0;
}
myTransform = transform.position;
}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "FlashlightOn"){
agent.speed = 20;
agent.SetDestination(target.position);
chasing = true;
}
}
public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) {
Vector3 randDirection = Random.insideUnitSphere * dist;
randDirection += origin;
NavMeshHit navHit;
NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);
return navHit.position;
}
}
Flashlight Script:
var isOn = false;
var flashlight : Light;
public var maxBatteryLife : float = 120;
public var curBatteryLife : float;
var cone : GameObject;
function Start()
{
curBatteryLife = maxBatteryLife;
cone.tag = "FlashlightOff";
}
function Update ()
{
if(Input.GetKeyDown("e"))
{
isOn = !isOn; // toggle it's state
}
if (isOn == true)
{
flashlight.intensity = 3;
curBatteryLife -= Time.deltaTime;
cone.tag = "FlashlightOn";
if (curBatteryLife <= 0)
{
isOn = false;
cone.tag = "FlashlightOff";
}
}
if (isOn == false)
{
flashlight.intensity = 0;
cone.tag = "FlashlightOff";
if (curBatteryLife <= 0)
{
isOn = false;
cone.tag = "FlashlightOff";
}
}
}
Player Script:
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var curStamina : float;
var maxStamina : float = 120;
var isSprinting = false;
var heartbeat : AudioClip;
var isBeating = false;
GetComponent.<AudioSource>().clip = heartbeat;
GetComponent.<AudioSource>().Play();
var beatVolume : float = 0;
GetComponent.<AudioSource>().volume = beatVolume;
private var moveDirection : Vector3 = Vector3.zero;
function Start()
{
curStamina = maxStamina;
isBeating = false;
}
function Update() {
var controller : CharacterController = GetComponent.<CharacterController>();
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
if(curStamina >= 0){
if(Input.GetButtonDown("sprint")){
if(curStamina > 0)
{
speed = 24.0;
isSprinting = true;
isBeating = true;
}
}
}
if(Input.GetButtonUp("sprint")){
speed = 12.0;
isSprinting = false;
isBeating = false;
}
if(isSprinting == true){
curStamina -= Time.deltaTime;
if(curStamina <=0){
isSprinting = false;
}
if(curStamina <=0){
isSprinting = false;
curStamina = 0;
}
if(curStamina >= maxStamina){
curStamina = maxStamina;
}
} if(isBeating == true)
{
if(beatVolume < 0.8)
{
beatVolume += 0.3 * Time.deltaTime;
GetComponent.<AudioSource>().volume = beatVolume;
}
}
if(isBeating == false)
{
if(beatVolume > 0)
{
beatVolume -= 0.2 * Time.deltaTime;
GetComponent.<AudioSource>().volume = beatVolume;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Thanks In Advance!
Answer by AmunTech · Nov 15, 2015 at 02:51 PM
Have your navmesh a collider attached? You need colliders to detect collisions
Thank you so much! I saw the "collider" that the navmesh had on it and assumed I had one.
Your answer
Follow this Question
Related Questions
Luz parpadeante con bateria 0 Answers
Change scene with trigger collision not working. 1 Answer
Realtime mesh deformation collisions in Unity 5 0 Answers
Check for collision while animating 0 Answers
Multiple Cars not working 1 Answer