Best Way To Detect a particular game object without Colliders

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:

    void OnTriggerEnter(Collider other)
    {
        //check if player zombie is within triggering range of character
        if (other.gameObject.tag == "PlayerZombie")
        {
            anim.SetBool("isScared", true);    //if player is too close, set character to "scared" state
            print("Zombie in range!");     //print statement for test
        }


    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "PlayerZombie")
        {
            anim.SetBool("isScared", false);   //make character not scared when player zombie is out of range
            //agent.ResetPath();
            print("Zombie out of range");      //test print
        }
    }

Here’s what I was trying to do now:

    //check the surrounding area for the player zombie
    void CheckForPlayer()
    {
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;
            if (Physics.SphereCast(ray, 20f, out hit, 20f))
            {
                if (hit.collider.gameObject.tag == "PlayerZombie")
                {
                    anim.SetBool("isScared", true);    //if player is too close, set character to "scared" state
                    print("Zombie in range!");     //print statement for test
                }
                else
                {
                    anim.SetBool("isScared", false);
                    print("Zombie out of range");
                }
            }     
    }

But the above is doing nothing at all. What am I doing wrong?

Never mind. I feel stupid. This problem was easily solved by using Vector3.Distance().

    //check the surrounding area for the player zombie
    void CheckForPlayer()
    {
        float playerDistanceFromCharacter = Vector3.Distance(player.transform.position, transform.position);
        Debug.DrawLine(player.transform.position, transform.position);
                if (playerDistanceFromCharacter < 20f)
                {
                    anim.SetBool("isScared", true);    //if player is too close, set character to "scared" state
                    print("Zombie in range!");     //print statement for test
                }
                else
                {
                    anim.SetBool("isScared", false);
                    print("Zombie out of range");
                }    
    }

Sorry to bug people. :slight_smile:

1 Like

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.

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!

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.

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: Unity 5 Modular Detector - Pastebin.com

I’m learning that stuff in the next couple days I hope. Thanks again.