Search Unity

Best Way To Detect a particular game object without Colliders

Discussion in 'Scripting' started by relic1882, Mar 29, 2015.

  1. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    I'm trying to do away with the giant sphere collider on my NPC that detects whether or not you are close. When you get too close, they will run away from you. When they are far enough away or you are, they go back to walking normally. The reason I want to use another method is because the sphere collider trigger is interfering with a raycast on another part I'm working on.

    Here's what I had set up before:

    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         //check if player zombie is within triggering range of character
    4.         if (other.gameObject.tag == "PlayerZombie")
    5.         {
    6.             anim.SetBool("isScared", true);    //if player is too close, set character to "scared" state
    7.             print("Zombie in range!");     //print statement for test
    8.         }
    9.  
    10.  
    11.     }
    12.  
    13.     void OnTriggerExit(Collider other)
    14.     {
    15.         if (other.gameObject.tag == "PlayerZombie")
    16.         {
    17.             anim.SetBool("isScared", false);   //make character not scared when player zombie is out of range
    18.             //agent.ResetPath();
    19.             print("Zombie out of range");      //test print
    20.         }
    21.     }
    Here's what I was trying to do now:
    Code (CSharp):
    1.     //check the surrounding area for the player zombie
    2.     void CheckForPlayer()
    3.     {
    4.             Ray ray = new Ray(transform.position, transform.forward);
    5.             RaycastHit hit;
    6.             if (Physics.SphereCast(ray, 20f, out hit, 20f))
    7.             {
    8.                 if (hit.collider.gameObject.tag == "PlayerZombie")
    9.                 {
    10.                     anim.SetBool("isScared", true);    //if player is too close, set character to "scared" state
    11.                     print("Zombie in range!");     //print statement for test
    12.                 }
    13.                 else
    14.                 {
    15.                     anim.SetBool("isScared", false);
    16.                     print("Zombie out of range");
    17.                 }
    18.             }    
    19.     }
    But the above is doing nothing at all. What am I doing wrong?
     
  2. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    Never mind. I feel stupid. This problem was easily solved by using Vector3.Distance().

    Code (CSharp):
    1.     //check the surrounding area for the player zombie
    2.     void CheckForPlayer()
    3.     {
    4.         float playerDistanceFromCharacter = Vector3.Distance(player.transform.position, transform.position);
    5.         Debug.DrawLine(player.transform.position, transform.position);
    6.                 if (playerDistanceFromCharacter < 20f)
    7.                 {
    8.                     anim.SetBool("isScared", true);    //if player is too close, set character to "scared" state
    9.                     print("Zombie in range!");     //print statement for test
    10.                 }
    11.                 else
    12.                 {
    13.                     anim.SetBool("isScared", false);
    14.                     print("Zombie out of range");
    15.                 }    
    16.     }
    Sorry to bug people. :)
     
    imkp425 likes this.
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    In the future you should just put the Sphere Collider on the "Ignore Raycast" layer.

    100+ enemies all checking the distance to the player might not be ideal.
     
  4. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    You make good points. I was hoping that something like this or Raycast would be simpler than using a Sphere Collider.

    How do I change the layer of the sphere collider? It seems the only way easily is to make another object a child of the NPC and set it to Ignore Raycast there. Will the OnTriggerEnter and Exit work the same with the collider as a child object? And will the script still work the same as long as it's attached to the parent?

    Thanks for the input!
     
  5. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    Thought I wasn't going to have time but I answered my own questions here. Thanks a lot for your help. It saved me time.
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    I put my detectors on their own child gameobject.

    There's a trick where if you add a Rigidbody (isKinematic true) to the child Sphere Collider, the parent scripts' OnTriggerEnter will get called, so you shouldn't have to change your code.

    But normally I just have a script that just sends some events anything can listen to: http://pastebin.com/mdGxpzQE
     
  7. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    I'm learning that stuff in the next couple days I hope. Thanks again.