- Home /
Change light when in trigger and pressing "E" ?
Hey everybody. I'm making a game that need a door to be unlocked using this "electricity box". When I come into the room the box's light is red, altough when I enter the trigger, the light turns green and the door unlocks. Everything there is ok.
But the problem is, that I want the light to turn green when I'm in the trigger and pressing E, not by just aproaching it.
Any help would be really nice. Thanks.
Oh and here is the script I got:
function Start(){
}
//Activate the Main function when player is near the box
function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag =="Player")
{
light.color = Color.green;
}
}
Answer by AlucardJay · May 11, 2014 at 08:58 AM
I have also added a boolean switchActivated which you can use to check if true to activate other things :
Updated answer Player toggles light on/off while standing in the trigger zone.
var soundTurnOff : AudioClip;
var soundTurnOn : AudioClip;
var playerInTrigger : boolean = false;
var switchActivated : boolean = false;
function Update()
{
// check the player is in the trigger zone
if ( playerInTrigger )
{
// check for player input
if ( Input.GetKeyDown( KeyCode.E ) )
{
// behaviour based on the state of switchActivated
switch ( switchActivated )
{
// if switchActivated == false, turn it ON
case false :
switchActivated = true;
light.color = Color.green;
audio.clip = soundTurnOn;
audio.Play();
break;
// if switchActivated == true, turn it OFF
case true :
switchActivated = false;
light.color = Color.red;
audio.clip = soundTurnOff;
audio.Play();
break;
}
}
}
}
function OnTriggerEnter(c:Collider)
{
if ( c.gameObject.tag == "Player" )
{
playerInTrigger = true;
}
}
function OnTriggerExit(c:Collider)
{
if ( c.gameObject.tag == "Player" )
{
playerInTrigger = false;
}
}
// ------------------------------------------------------------------------------------
original answer that automatically turned off when player left the trigger.
var soundTurnOff : AudioClip;
var soundTurnOn : AudioClip;
var playerInTrigger : boolean = false;
var switchActivated : boolean = false;
function Update()
{
if ( playerInTrigger && !switchActivated )
{
if ( Input.GetKeyDown( KeyCode.E ) )
{
switchActivated = true;
light.color = Color.green;
audio.clip = soundTurnOn;
audio.Play();//It plays the TurnOn sound
}
}
if ( !playerInTrigger && switchActivated )
{
switchActivated = false;
light.color = Color.red;
audio.clip = soundTurnOff;
audio.Play(); //It plays TurnOff sound
}
}
function OnTriggerEnter(c:Collider)
{
if ( c.gameObject.tag == "Player" )
{
playerInTrigger = true;
}
}
function OnTriggerExit(c:Collider)
{
if ( c.gameObject.tag == "Player" )
{
playerInTrigger = false;
}
}
EDIT: No. It seems that when I leave the trigger, it automaticly turns back to red. EDIT 2: Fixed it, but now when I leave the trigger, the sound gets turned off.
Ok, it seems I misunderstood the behaviour you wanted.
The original answer turned the light off when the player left the trigger zone.
The updated answer turns the light on or off when the player is in the trigger zone. The light stays in the selected state even when the player leaves the zone.
Thanks! Thats excatly what I wanted. I'll keep this in $$anonymous$$d.
P.S: Is there a way to play another sound when you switch to state "on" ? It's additional question, don't have to answer it, but it would help me alot.
Without having another audioSource, you could use PlayOneShot or PlayClipAtPoint
Answer by gdubrocks · May 11, 2014 at 06:04 AM
Right now your code activates any time your player enters the area.
You want to check if a key was pressed, so use Input.getKey()
function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag =="Player")
{
if (Input.GetKey ("e"))
{
light.color = Color.green;
}
}
}
This explains it better http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html
I tried your code, but for some wierd reason it doesn't work. I enter the trigger, the sound still plays, the door unlocks on it's own without me even pressing $$anonymous$$ Here, I edited the script a bit:
var soundTurnOff : AudioClip;
var soundTurnOn : AudioClip;
function Start(){
}
//Activate the $$anonymous$$ain function when player is near the box
function OnTriggerEnter(c:Collider)
{
if(c.gameObject.tag =="Player"){ //If the player enters the trigger..
audio.clip = soundTurnOn;
audio.Play();//It plays the TurnOn sound
light.color = Color.green;
} else { //If player is not in trigger...
audio.clip = soundTurnOff;
audio.Play(); //It plays TurnOff sound
}
}
Never check for a key in a trigger/collision event. This has been covered many times before. Unless you are psychic or have amazing reflexes, the chance that you will press a key at the exact moment of the event is never.
@poff you never even added the input condition :/
have a boolean that is toggled by trigger enter/exit events, then in update check if the boolean is true and key is pressed down.
@alucardj out of interest, is it okay to check key presses in OnTriggerStay or is this bad for performance reasons? P.S. Once I'm home I can check this myself so don't worry if you miss this.
http://docs.unity3d.com/Documentation/ScriptReference/Input.html
Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop.
As trigger events occur at different intervals to Update, it is not recommendable.
Your answer
Follow this Question
Related Questions
Activate an input to pickup or search objectes 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Input seemingly not working with trigger 1 Answer
Random Trigger placement? 1 Answer
jumpscare only ince ? 1 Answer