Use Collider like a radar...

Hey guys,

I am having major issues with the collider/collision concept it seems and I need a few clarifications.

The concept I am trying to make is like a submarine boat that uses sonar to detect other ships in the ocean. The boat itself has a collider (like a tube for its shape).

I added an empty GameObject, gave it a rigidbody component with zero mass and ‘Is Kinematic’ = true; and then added a sphere collider to the empty GameObject that can be adjusted to reach many hundreds of feet if necessary. I made this empty GameObject a child object of the boat.

Ideally, what I want to know is this:

  1. If two sonar detection signals (sphereColliders) bump into each other, to detect a collision and report what transform the boat is (by using the parent information of course)
  2. Do the OnCollisionEnter/Stay/Exit functions only work when the collision happens on the surface of the Collider? what happens if something is already inside the bounds of the collider? ( I ask this because I’m trying to report using Debug.Log(contact.otherCollider.name) and there is nothing to report even though my sample boats are all within range of each other).

I need some help with code that actually works because my usage of OnCollisionEnter and such is doing nothing at all in regards to collider detections.

// Code //

public SphereCollider sonarCollider;

start(){sonarCollider.enabled = true;}

void OnCollisionStay(Collision collision) {

        foreach (ContactPoint contact in collision.contacts) {

Debug.Log(contact.thisCollider.name + " hit " + contact.otherCollider.name);

            print(contact.thisCollider.name + " hit " + contact.otherCollider.name);

            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
    }

… and yet nothing is happening…

Be sure to always format your code with the 101010 button. I fixed it for you because it’s a good question. :wink:

Edit:

My original answer was quite thoroughly incorrect. Evidently I haven’t been keeping up with the changelogs, because in the past, triggers did not respond to one another. Now apparently they do; I just tested and confirmed this. Lucky for you, because my old workaround technique was rather convoluted.

Anyhow, your “sonar” colliders should be triggers. These should have a kinematic rigidbody attached. When two overlap, they will both fire their OnTriggerEnter events.

By the way, is this a real phenomenon? Can one sonar device detect the activity of another if their fields overlap? Certainly your sonar trigger should respond when a non-trigger collider enters their trigger collider, but to other sonar triggers as well?