Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Player4 · Nov 15, 2015 at 08:14 AM · c#javascriptcollisionenemyaiflashlight

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AmunTech · Nov 15, 2015 at 02:51 PM

Have your navmesh a collider attached? You need colliders to detect collisions

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Player4 · Nov 16, 2015 at 09:28 PM 0
Share

Thank you so much! I saw the "collider" that the navmesh had on it and assumed I had one.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges