- Home /
How to play sound when object stops moving?
I'm trying to make a sound play when a vehicle stops moving. Please help me.
Answer by TanselAltinel · May 07, 2018 at 12:39 PM
First of all, you need to know when your object stops. In order to get to that point, there are two solutions:
If you are using Unity physics, you can check rigidbody.velocity to see if it is a zero vector, and play a sound at that time.
If you are moving your object with updating transform values, then you can check this answer to see a possible solution: https://answers.unity.com/questions/739505/how-to-check-if-an-object-has-stopped-moving.html
Finally, you can keep a flag to check to make sure when to play sound like this:
// your code to understand if object stopped, then set the flag (bool isObjectStopped) to true
if(isObjectStopped && !isMusicPlayed) {
// play sound here
isMusicPlayed = true;
}
// when your object starts to move again, set the flag (bool isObjectStopped) to false
Your answer
Follow this Question
Related Questions
Car engine sound 1 Answer
Sprinting Audio Problem 1 Answer
Do mixer.TransitionToSnapshots' weights need to sum to 1? 0 Answers
audio doesn't play on movement 0 Answers
Articulated Vehicle 0 Answers