Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

trouble with triggers

Discussion in 'Scripting' started by herman111, Sep 8, 2015.

  1. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    I'm trying to get a model to stop walking, when it enters a Collider trigger, then play the attack animation...I have this script and a circle Collider on the model set to IsTrigger....when it hits the player its supposed to stop, but doesn't...The Player is tagged with player...the animation state is set to a bool,when the walking animation is played the bool is false, when the trigger is touched, the bool is true, then the attack is played.
    What am I doing wrong?..The model walks right into the player and the trigger isn't working, until I back off the player a little, then it plays...the trigger just isn't triggering like its suppose to. this is all happening on a NavMesh and I'm using Unity 4.6
    .





    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class zombieIdleTrigger : MonoBehaviour
    5. {
    6.  
    7.     Animator anim;
    8.     NavMeshAgent agent;
    9.     Transform Player;
    10.  
    11.     void Awake()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.         agent = GetComponent<NavMeshAgent>();
    15.         Player = GameObject.FindGameObjectWithTag("Player").transform;
    16.         anim.SetBool("zombieStop",false);
    17.     }
    18.    
    19.     void OnTriggerEnter (Collider other)
    20.     {
    21.         if(other.gameObject.tag =="Player")
    22.         {
    23.             anim.SetBool("zombieStop",true);
    24.             agent.speed = 0;
    25.             Debug.Log("ENTERED");
    26.         }
    27.     }
    28.  
    29.  
    30.  
    31.     void OnTriggerExit (Collider other)
    32.     {
    33.         if(other.gameObject.tag =="Player")
    34.         {
    35.             anim.SetBool("zombieStop",false);
    36.             agent.speed = 3;
    37.             Debug.Log("EXIT");
    38.         }
    39.     }
    40. }
    41.  
     
  2. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class zombieIdleTrigger : MonoBehaviour
    5. {
    6.  
    7.     Animator anim;
    8.     NavMeshAgent agent;
    9.     Transform Player;
    10.     public navmeshImproved navScript;
    11.     public int ZombieSpeed = 0;
    12.  
    13.     void Awake()
    14.     {
    15.         anim = GetComponent<Animator>();
    16.         agent = GetComponent<NavMeshAgent>();
    17.         Player = GameObject.FindGameObjectWithTag("Player").transform;
    18.         anim.SetBool("zombieStop",false);
    19.     }
    20.    
    21.     void OnTriggerEnter (Collider other)
    22.     {
    23.         if(other.gameObject.tag =="Player")
    24.         {
    25.             anim.SetBool("zombieStop",true);
    26.             navScript.enabled = false;
    27.             agent.enabled = false;
    28.             agent.speed = 0;
    29.             Debug.Log("ENTERED");
    30.         }
    31.     }
    32.  
    33.  
    34.  
    35.     void OnTriggerExit (Collider other)
    36.     {
    37.         if(other.gameObject.tag =="Player")
    38.         {
    39.             anim.SetBool("zombieStop",false);
    40.             navScript.enabled = true;
    41.             agent.enabled = true;
    42.             agent.speed = ZombieSpeed;
    43.             Debug.Log("EXIT");
    44.         }
    45.     }
    46. }
    47.  
    nevermind...needed more stuff in script
     
    Last edited: Sep 9, 2015