Search Unity

Problem with 2 colliders

Discussion in 'Scripting' started by GamesOnAcid, Feb 11, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    I'm using a script to trigger a collider, and another one to trigger a different one. One makes it so that when you enter, an enemy runs towards you. The other script makes it so that when he gets close, he damages you. Problem is, when I run the game, the first collider makes him run, and the second makes him walk (since they are inside each other.) They are seperate GameObjects, but one is a child of the enemy. How do I make it so that he can still damage you in a certain radius, but not so that the animation change is triggered as well?
    Trigger on Enemy(Handles Animations)
    Code (CSharp):
    1.     void OnTriggerEnter(Collider col){
    2.        
    3.         if(col.gameObject.tag == "Player"){
    4.             anim.SetFloat ("Speed", 3f);
    5.             anim.SetBool("Running", true);
    6.             anim.SetBool("Walking", false);
    7.         }
    8.     }
    9.  
    10.     void OnTriggerExit(Collider col){
    11.         if(col.gameObject.tag == "Player"){
    12.             anim.SetFloat ("Speed", 1.5f);
    13.             anim.SetBool("Running", false);
    14.             anim.SetBool("Walking", true);
    15.         }
    16.     }
    17.  
    Child of Enemy(Handles Damage)
    Code (CSharp):
    1. function OnTriggerEnter (Col : Collider)
    2. {
    3.     if(Col.tag == "Player")
    4.     {
    5.         SanityDamage.currentSanity += scarePercent;
    6.         GetComponent.<AudioSource>().PlayOneShot(ScareSound);
    7.         hasBeenTriggered = true;
    8.     }
    9. }
    10.  
    11. function OnTriggerExit (Col : Collider)
    12. {
    13.     if (Col.tag == "Player")
    14.     {
    15.         SanityDamage.currentSanity = SanityDamage.currentSanity;
    16.     }
    17. }
    18.  
     
  2. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    have you thought of using distance calculations instead of colliders. Make the outside "detection" a collider and then you can calculate the distance to set the run walk and damage parts.
    Code (CSharp):
    1. float distance = Vector3.Magnitude(transform.position - other.transform.position);
    you really want to watch using costly colliders and having a lot of colliders gets very confusing very fast. When I do enemy AI they have a detection radius( sphere collider set to trigger) and then check their distance to find out what they should do, ie. charge, attack, shoot, toss a grenade, take cover, whatever.