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 Batman009876 · Feb 16, 2014 at 08:56 PM · triggermusicradio

How to play a song when a key is pressed down within a trigger? Trying to make a Radio :P

Hello Unity Community! Im in need of some help. Im trying to make it so when you enter a trigger (An object with a trigger) and press a Key (Would like it to be "E") to turn the song on. and you can leave the trigger. And if you want to turn it off you can enter the trigger again and press the same key you used to turn it on. Can someone please tell me how to do it? I would love a video showing how to write the code. Because I am REALLY not the best at coding. Please help me :D -Stuart P.S I am not sure if you need to use triggers or not. Im just guessing :P Thanks -Stuart

Comment
Add comment · Show 4
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 Klarax · Feb 17, 2014 at 09:37 AM 0
Share

Are you asking to:

have an area where you can press a button and song plays ? (inside a trigger)

[Please check syntax etc as i done this from memory :P]

it would be something along the lines of:

     private isPlaying = false;
     public AudioClip YOUR_SOUND;
     
     void OnTriggerEnter(Collider col)
     {
        if(Input.GetButtonDown(BUTTON YOU WANT) && isPlaying == false)
        {
           AudioSource.PlayClipAtPoint(YOUR_SOUND, transform.position);
     
           isPlaying = true;
        }
        else if(Input.GetButtonDown(BUTTON YOU WANT) && isPlaying == true)
        {
          audio.Stop();
     
           isPlaying = false;
        }
     }
avatar image Klarax · Feb 17, 2014 at 09:55 AM 0
Share

BTW, you may want to add a tag to the function testing if player is the object in the box. This would go before the input.getbuttondown function

avatar image AlucardJay · Feb 17, 2014 at 12:37 PM 0
Share

Checking if a key is pressed at the exact moment a trigger is entered never works, you would need split-second ti$$anonymous$$g and reflexes. This could be done in OnTriggerStay, but checking for inputs in a trigger function is never recommended. You are better off using a boolean to tell if you are in a trigger zone, then check for the key input in an update.

avatar image Batman009876 · Feb 17, 2014 at 05:23 PM 0
Share

Ok. So I tried your code. Got a few erros. Got a friend to help fix them. I add these lines of code to my record player var isPlaying = false; var Shake : AudioClip;

 function OnTriggerEnter (other : Collider) {
  if(Input.Get$$anonymous$$ey("e") && isPlaying == false)
  {
   AudioSource.PlayClipAtPoint(Shake, transform.position);
   isPlaying = true;
  }
  else if(Input.Get$$anonymous$$ey("r") && isPlaying == true)
  {
   audio.Stop();
    
    isPlaying = false;
  }
 }

I walk up to it and press "E" But nothing happens :O any sugestions?

2 Replies

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

Answer by AlucardJay · Feb 17, 2014 at 12:38 PM

From my comment : You are better off using a boolean to tell if you are in a trigger zone, then check for the key input in an update.

Untested uJS pseudocode :

 var inTrigger : boolean = false;
 var isPlaying : boolean = false;
 
 function Update()
 {
     // check if in the trigger zone
     if ( inTrigger )
     {
         // check if key has been pressed
         if ( Input.GetKeyDown( KeyCode.E ) )
         {
             // check if music is already playing
             if ( isPlaying )
             {
                 // turn music OFF
                 audio.Stop();
                 isPlaying = false;
             }
             else // not playing
             {
                 // turn music ON
                 audio.Play();
                 isPlaying = true;
             }
         }
     }
 }
 
 function OnTriggerEnter( other : Collider )
 {
     // check if the MusicTrigger is the trigger thats been entered
     if ( other.gameObject.name == "MusicTrigger" ) 
     {
         inTrigger = true;
     }
 }
 
 function OnTriggerExit( other : Collider )
 {
     // check if the MusicTrigger is the trigger thats been exited
     if ( other.gameObject.name == "MusicTrigger" )
     {
         inTrigger = false;
     }
 }
Comment
Add comment · Show 1 · 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 Batman009876 · Feb 17, 2014 at 05:28 PM 0
Share

YES, THAN$$anonymous$$ YOU, It worked!!

avatar image
0

Answer by mattyman174 · Feb 17, 2014 at 12:23 PM

Probably a better way would be to detect if the Player is looking at the Radio via a RayCast and if they are looking at it and within a certain maximum distance away from the Radio you can allow them to press a Key to turn it on.

Use the Unity Scripting Reference linked below to help you find helpful functions to reach your goal.

http://docs.unity3d.com/Documentation/ScriptReference/

Here are some links to some of the Components and Functions you may need to use.

Audio Source

RayCast

Vector3.Distance

Comment
Add comment · 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

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

21 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

Related Questions

How to have audio only be heard in a certain radius? 4 Answers

Attack Animation Trigger 1 Answer

How to check if Trigger is 'empty'? 6 Answers

Can't click gameobject when over another trigger? 1 Answer

Interactive Tracker Music? 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