I want to trigger an event when there is another object behind the object that I selected with mouse. Can you help me with the functions that I need and how to apply them. Thanks.
1 Answer
1You should make a script with the following code:
But before you do that, MAKE SURE you make a trigger and attach this script to it
var objectBehind : GameObject; //this is the object that will be behind your other object
function OnTriggerEnter (Other : Collider){
if(Other.gameObject.tag == "yourTag") {//tag the other object to a tag and change "yourTag" to that tag
//Do something, implement your code here or animation or whatever
}
}
You should put a short and explanatory title and ask your question in the body.
– TanshaydarPretty fuzzy question. If you mean anywhere behind, then you can use Transform.InverseTransformPoint() on all potential game objects and take a look 'z' values. If you mean approximately directly behind, you can Raycast backwards. Or depending on your situation, you could compare the angle of the forward vector to a ray between potential objects and the selected object.
– robertbu@rubertbu, wouldn't it be much efficient to just compare the distance, rather than doing a whole matrix multiplication in order to do the same? (
– Benproductions1Vector3.DistancenotTransform.InverseTransformPoint)The thing is the objects are always changing. The one I select and the one that is behind it according to my fps position. So I need something that is valid for all cases.
– skececi