|
I want to have a cube in my world. When the player is near the cube, I want to display some text instructing the player what to do. The only way that I can seem to accomplish this is by:
(comments are locked)
|
|
You have several ways to do that. You can check the distance from the cube and do whatever you want when the player is below certain distance, or you can create a cubic trigger and use the OnTriggerEnter to fire your message. Anyway, using FindWithTag (or other Find flavors) is by far the worse - it takes an appreciable amount of time to check all objects, thus you'd better to use any Find routine only at Start (the docs recommend this).
bool guiOn = false;
void OnTriggerEnter(Collider obj){ // turn message on when player is inside the trigger
if (obj.tag == "Player") guiOn = true;
}
void OnTriggerExit(Collider obj){ // turn message off when player left the trigger
if (obj.tag == "Player") guiOn = false;
}
void OnGUI(){
if (guiOn){ // only show message if guiOn is true
// draw text here
}
}
Thank you, but how would I tell Unity that I want the "OnTriggerEnter" to be linked with my cube?
Sep 21 '11 at 11:30 PM
xSpectrum
It says in the post- OnTriggerEnter gets called if you set your collider to be a trigger! That's what happens when you set a collider to be a trigger.
Sep 21 '11 at 11:45 PM
syclamoth
This script must be assigned to the cube: when the trigger is entered, the scripts attached to the trigger object will have their OnTriggerxxx functions called.
Sep 22 '11 at 02:22 AM
aldonaletto
(comments are locked)
|
