Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by poff · May 11, 2014 at 04:45 AM · inputtriggerlighthorrore

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;
        
        }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image poff · May 11, 2014 at 09:13 AM 0
Share

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.

avatar image AlucardJay · May 11, 2014 at 11:00 AM 0
Share

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.

avatar image poff · May 11, 2014 at 11:34 AM 0
Share

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.

avatar image AlucardJay · May 11, 2014 at 11:41 AM 0
Share

Without having another audioSource, you could use PlayOneShot or PlayClipAtPoint

avatar image poff · May 11, 2014 at 11:42 AM 0
Share

I got it working, thanks dude.

avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image poff · May 11, 2014 at 08:05 AM 0
Share

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
        
 }
             }
avatar image AlucardJay · May 11, 2014 at 08:55 AM 0
Share

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.

avatar image Scribe · May 11, 2014 at 11:03 AM 0
Share

@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.

avatar image AlucardJay · May 11, 2014 at 11:09 AM 0
Share

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.

avatar image Scribe · May 11, 2014 at 11:18 AM 0
Share

Ok cool, that makes sense. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges