- Home /
Question by
Chr15_v101 · Feb 05, 2017 at 06:34 AM ·
c#gameobjectaudio
Make sound play at same time as object moves
Hi.
I have an elevator platform which rises and lowers when the player enters or leaves the collider. I can get the sound to play but it willobly play when the movement has completed.
How can I make the sound play as the elevator moves?
Would I need to use a separate thread?
Comment
Answer by WoozyBytes · Feb 05, 2017 at 11:35 AM
@ Chr15_v101Hi!
One way would be to start playing the sound OnCollisionStay if is not already playing, and stop playing when the player leaves the elevator OnCollisionExit
using UnityEngine;
using System.Collections;
public class ElevatorSoundScript : MonoBehaviour {
AudioSource elevatorAudioSource ;
void OnCollisionStay ( Collision collisionStayInfo ) {
if( collisionStayInfo.gameObject.tag == "Player" ) {
if(elevatorAudioSource.isPlaying == false){
elevatorAudioSource.Play();
}
}
}
void OnCollisionExit ( Collision collisionExitInfo ) {
if( collisionExitInfo.gameObject.tag == "Player" ) {
if(elevatorAudioSource.isPlaying == true){
elevatorAudioSource.Stop(); // may be better to fade out the sound before stopping
}
}
}
}
Hope it Helps , Regards
Your answer
Follow this Question
Related Questions
audio source not playing the audio clip 2 Answers
this.GameObject - Uses? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Volume Control with Slider 1 Answer