- Home /
How to make an Object react if a key is hit within a trigger?
Hey, I'm making a game where I need a gameobject to react when E (Or any button) is pressed within the area of a trigger collider. I don't know if there is any specific command far "While inside trigger", and if there is one, I have yet to find it. I'd appreciate anyone's help, thank you.
Answer by sparkzbarca · Apr 03, 2015 at 12:32 AM
use a bool to know when someone is in a trigger.
for example.
void OnTriggerEnter (collider object)
{
if (object.tag == "player")
DoReact = true;
}
void OnTriggerExit (collider object)
{
if (object.tag == "player")
DoReact = false;
}
if (input.keycode('e') && DoReact)
{
...do something
}
when i put this in, object is an error, as an unexpected declaration in struct and etc, the == are also errors, and and the () around collider object
I believe @sparkzbarca made the assumption that you would have sufficient experience to recognize some shorthand and pseudocode. Please consider conducting ample research to familiarize yourself with Unity and C# prior to asking for help.
Usually, you'll want to know WHAT object is within the trigger and respond to input only if the object is of the correct type:
bool is$$anonymous$$eyDown;
void Update() {
is$$anonymous$$eyDown = Input.Get$$anonymous$$ey( $$anonymous$$eyCode.E );
}
void OnTriggerStay( Collider other ) {
// here, you'd check some property of the "other" object
if ( otherObjectIsTheRightObject && is$$anonymous$$eyDown ) {
// do whatever
}
}
I wish i had seen this sooner, I did end up solving the issue though, so many thanks to the both of you.
Your answer

Follow this Question
Related Questions
Adding gameObjects colliding with a trigger to an array 2 Answers
Inconsistent Trigger events 1 Answer
count the number on enemyes entrering into a trigger 1 Answer
Best Way to Find nearby colliders when you already have a trigger collider on gameObject 1 Answer
Does OnTriggerStay ignore layers? 1 Answer