Distance between Objects (one is a child)

Hello *,

I have the problem that I will create a couple of random prefabs (two sorts, radar and opponents). The hierachy of the radar is:

Radar (empty game object)
…> Trigger (empty game object)
…> Sphere_Collider (Sphere Collider) with tag “RadarTag”
…> Detector (Mesh)

(Sphere_Collider is a child of Trigger)

The detector moves inside the Trigger. Now if an opponent enters the trigger a script should check the distance between the sphere collider and the opponent.

The opponent has a script with the following source:

     function OnTriggerStay (other: Collider){
	if (other.CompareTag("RadarTag")) {
		Debug.Log("hit");
		if (Vector3.Distance(other.transform.position, xxx.transform.position) < 4)
			Debug.Log("in Range"); 
		else
			Debug.Log("out of Range");
	}
      }

Please can you tell me what I should enter instead of: xxx.transform.position (something like other.childWithName(“Detector”) or so).

Thank you,
Ulrich

Can you clarify:
What is ‘other’? Is it ‘Sphere_Collider (Sphere Collider) with tag “RadarTag”’ ?
What is ‘xxx’ meant to be? The opponent or the Detector?

If you want the oponent to check if he’s near the object, remove the xxx.
The local transform of the gameobject the script is attached to can be used just fine, unless you have other plans.

sorry, that was a mistake. I wanted to write:

if (Vector3.Distance(other.xxx.transform.position, transform.position) < 4)…

where other.xxx should be the Detector (child of the Trigger).

Other is the Sphere_Collider

Thew script is attached to the opponent and I have no idea how to find the Detector which is a part of the Radar. Sphere_Collider is a child of Radar/Trigger but Detector should not become a child of Sphere_Collider because it will move around inside the Sphere_Collider. Detector is a child of Radar. Phew, you know what I mean?

I have uploaded a little sample now which shows what I mean (9 kb). You can download and have a look at: Simulation von Geldspielautomaten / Geldspielgeräten - Virtuelle Spielautomaten und Geldspielgeräte. This sample should explain what I need to know. I would be glad if you could have a look at it.

Thank you for your help,
Ulrich

You could use something like this:

other.gameObject.Find(“Detector”).transform. etc

where Detector is the name of the gameobject.
If you’re using dynamic names in the future, you can tag them and use FindByTag(or something similar)

Hello *,

I do not want to use a find-command because finding needs much time. Also I do not need to search becaudse when the collider is hit then there is always only one special “Detector”. So I tried the following: I have attached a script to the Sphere_Collider which only contains the variable

var detector: Transform;

In the editor I have drag and dropped the Detector to this variable and so I should find the transformation of the detector in a much easier way like:

if (Vector3.Distance(other.detector.transform.position, transform.position) < 4)…

But this won’t work… Please can you tell me what the correct syntax is for doing so?

Thank you,
Ulrich

if (Vector3.Distance(other.detector.transform.position, transform.position) < 4)…

You have a varible called detector. So where did ‘other’ come from?

And sicne its already a transform, why are you looking for it’s transform? A transform cannot have a transform child!

It should be:

if (Vector3.Distance(detector.position, transform.position) < 4) {//do stuff}…

(Only had a quick look so could be wrong).

other comes from :

function OnTriggerStay (other: Collider){
}

and has detector attached as a child.

The complete function is

function OnTriggerStay (other: Collider){
	if (other.CompareTag("RadarTag")) {
		Debug.Log("hit");
		if (Vector3.Distance(transform.position, other.detector.position) < 4)
			Debug.Log("in Range"); ;
	}
}

but neither “other.detector.position” nor “detector.position” will be accepted. The given error is " BCE0019: ‘detector’ is not a member of ‘UnityEngine.Collider’.

Ulrich

You’ll need to add a script to the detector’s parent if it’s somehow unacceptable to just move the trigger collider to the detector.

Radar.js

var detector : Transform;

Which you can then access in the OnTriggerStay using -

var r : Radar = other.GetComponent( Radar );
if ( r ) /* use r.detector here */

@Vicenti,

thank you! That was exactly what I was looking for :slight_smile:

Ulrich