- Home /
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
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;
}
}
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
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.
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?
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;
}
}
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.
Your answer
Follow this Question
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