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).
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.
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 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?
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’.