Unity sound when cube hit wall - audio

I have sound source and my question is how can I play that sound when my cube hit a wall?
I tried something like this, but it doesn't work.
usingUnityEngine;
usingSystem.Collections;
public class sound : MonoBehaviour {
voidOnTriggerEnter (Colliderother)
{
if(other.gameObject.tag == "wall")
{
Audio.PlayOneShot(sound);
}
}
}
I hope that someone will answer me. It's too important for me. Last few days I searched the whole Internet, I did so many changes on my game, but nothing work :(
Kind regards

usingUnityEngine;
usingSystem.Collections;
public class sound : MonoBehaviour {
public AudioSource soundEffect;
voidOnTriggerEnter (Collider other)
{
if(other.gameObject.tag == "wall")
{
soundEffect.Play ();
}
}
}
Assign audio source to script in inspector and assign script to cube.

Related

LibGdx How to make a sound play only at start of overlap between Actors?

I'm trying to make an Ouch! sound play at only the start of an overlap between a ball and wall Actors. So if the overlap is maintained I don't want the sound to play unless actors stop overlapping and then overlap again.
Here is part of the code where I'm trying to play the ouch sound:
for (BaseActor wallActor : BaseActor.getList(mainStage, "com.mygdx.game.WallBlock")){
ball.preventOverlap(wallActor);
ouch.play();
}
Please provide an example of how I could make this work.
You didn't posted much code but the solution is simple, keep a boolean overlapping in your ball actor and only when it changes from false to true play the sound.
Pseudocode:
public class Ball
{
private boolean overlapping;
private YourGame game; //could be a screen too or simply a world object
// other ball fields ...
public void update()
{
boolean wasOverlapping = overlapping;
overlapping = isOverlapping();
if(!wasOverlapping && overlapping)
ouch.play();
if(!overlapping && wasOverlapping)
{
//you could also play a sound when it stops overlapping here!
}
}
private boolean isOverlapping()
{
for(Wall wall : game.getWalls())
{
if(wall.overlap(this)) //this refers to the ball
return true;
}
return false;
}
}

Unity sound not playing

I'm trying to play a sound, but it's not playing
Here's my code:
public void Replay()
{
playAudio ();
Application.LoadLevel (Application.loadedLevel);
}
void playAudio()
{
AudioSource audio = GetComponent<AudioSource> ();
audio.Play();
}
When a button clicked, I'm calling Replay(). But, the sound is not played.
If I remarkedApplication.LoadLevel (Application.loadedLevel);, the sound plays normally.
What should I do to make the sound play with Application.LoadLevel()?
The AudioSource playing the sound will get removed before it has time to finish.
Here is an alternative solution using yield to wait for the sound to finish.
public void Replay()
{
StartCoroutine("ReplayRoutine");
}
IEnumerator ReplayRoutine()
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
Application.LoadLevel(Application.loadedLevel);
}
You call play method and you load the scene after it. Try to call playAudio() before loading level.
public void Replay() {
Application.LoadLevel(Application.loadedLevel);
playAudio();
}
I think you don't give the chance to the audio source to play the sound, because after executing play, you immediately re-loaded the scene, so the same instance of audio source does not exist anymore, a new instance is created which has not yet received the play command. You can use some small delay using co-routine or other ways to give the needed time to the audio source. ( If you want to play the sound before loading level, otherwise just play the sound in a Start() callback)

Unity sounds not playing after returning to previous scene

I have this class
public class MenuSounds : MonoBehaviour {
public AudioClip[] sounds = new AudioClip[1];
public void PlaySound(int sound)
{
Debug.Log("Playing sound");
Debug.Log("sounds[0]="+sounds[0]);
AudioSource.PlayClipAtPoint(sounds[0],Vector3.zero,1);
Debug.Log("After sound");
}
}
So the game starts and a main menu opens. There are buttons with Event triggers which run PlaySound(int s). That works fine until I change to other scene (main game) and then return to the Main_Menu scene.
Tried to debug... everything seems just fine. What am I doing wrong?
Thank you.

Key Input in javaFx 2.0

I am currently working on a javaFX 2.0 game and I want to know how to make it so when I type a specific key something will happen. Thank You for taking your time for reading this.
Make your root node focused and then write your logic in the setOnKeyPressed method.
Sample code:
root.setFocusTraversable(true);
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent kEvent) {
if (kEvent.getCode() == KeyCode.SPACE) {
System.out.println("Space Bar pressed");
}
}
});

media metadata observable map is null in java fx media

Hi guys I tried to play a song and print its metadata using java fx 2.1 api using the following code
Media media = new Media(UIandControls.class.getResource("/assets/testData/mom.mp3").toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(media);
ObservableMap list=media.getMetadata();
System.out.print(list);
mediaPlayer.play();
for some reason getMetadata() is empty observable Map.
Output looks like this
{}
plz help me.. Thank you.
Need to call mediaPlayer.play() before retrieving metadata.
I had troubles with this at first but I found that using this worked.
mediaPlayer.getMedia().getMetadata().addListener(new MapChangeListener<String, Object>(){
#Override
public void onChanged(Change<? extends String, ?> change) {
if(change.wasAdded()){
if(change.getKey().equals("artist"))
setArtist(change.getValueAdded().toString());
else if(change.getKey().equals("title"))
setTitle(change.getValueAdded().toString());
else if(change.getKey().equals("year"))
setYear(change.getValueAdded().toString());
}
}
});
Also I read that the media data won't always be there right after initialization, therefore calling the mediaPlayer.setOnReady() method can make sure that the data is there.
Example:
mediaPlayer.setOnReady(new Runnable(){
#Override
public void run() {
setTrackLength(mediaPlayer.getMedia().getDuration().toMinutes());
mediaPlayer.currentTimeProperty().addListener(progressChangeListener);
}
});

Resources