- Home /
How to create a rolling sound that only plays when object is moving?
i have finished the roll a ball tutorial and am trying to add a rolling sound effect. I don't want the sound to play when the ball is moving and it would be great if it could get louder, the faster the ball is moving.
Answer by JimmyCushnie · Aug 15, 2017 at 04:30 AM
To detect if an object is moving you could compare it to its position during the previous FixedUpdate. Would look something like this:
private Vector3 PreviousPosition;
void FixedUpdate(){
if(PreivousPosition != transform.position)
{
// play sound
}
PreviousPosition = transform.position;
}
rolling sound is now playing when game starts but as soon as the ball starts to roll, the sound stops?
Well that's because you suck the D with a capital D and need help
If the nosie is playing at the start of the game and stops as soon as the ball moves, then changing the != to == mad it work for me.
Answer by EXTORB · Aug 15, 2017 at 04:33 AM
Just use this script when trying to do it and it will work using UnityEngine;
public class ScriptExample : MonoBehaviour { void Start() { // Sets the position to be somewhere inside a circle // with radius 5 and the center at zero. Note that // assigning a Vector2 to a Vector3 is fine - it will // just set the X and Y values. transform.position = Random.insideUnitCircle * 5; } }
Your answer
Follow this Question
Related Questions
Making sound get louder as player moves faster? 1 Answer
Adding Audio 2 Answers
Ricochet and wallhit sounds 0 Answers
Sound won't start from "new" classes/objects -2 Answers