In the game there’s a ball colliding with a wall. I want the volume of the sound that is being played when this happens to be proportional to the speed with which the ball hit the wall.
But, for some reason the volume of the sound being played is the volume of the sound the previous time the volume was changed.
Example:
Default volume is 0.5f;
Ball collides at “full” speed. Volume gets set to 1.0f. Volume of the sound being played is 0.5f.
Ball now collides with a speed that sets the volume to 0.3f. The volume of the sound being played is 1.0f.
This despite a Debug.Log() states the volume is accurate. Just listening to the sound makes it obvious it is not so. Why?
I have to admit, I don’t really understand why the previous velocity is actually needed for this at all. Can you give any more explanation about how you are creating the sound effect?
Ah. Well, at the end of the update method the following line exist
prevVelocity = currVelocity;
I should’ve included that. Mistake on my part.
Uhm. The sound effect. I’ve attached an audio source to the ball, and whenever the if-statement in my original post evaluates to true it’s played.
And, maybe I’m missing something obvious here, but doesn’t it have to be dependent on the previous velocity? I mean, at the time of impact, the balls current velocity will be pretty much zero.
Here’s my thinking:
Frame n:
Velocity of ball is 76.0 and previous velocity is, well, 66.0f, to take a random number. No sound is supposed to be played. We update previous velocity so now it’s 76.0f.
Frame n+1:
Velocity of ball is 2.0f (huge drop in speed means collided) and previous velocity is 76.0f. Time to play a sound. We need the volume of the sound to reflect the speed the ball had before it collided, which is the previous velocity. Set volume, play sound. But for some reason the volume of the sound actually is the volume it already had, THEN it gets updated with the new volume.
Even if my approach is bad, I still fail to see why the volume of the sound being played is the volume the audio source already had.
And any tips for improved way of dealing with this would be appreciated too. =)
Cannot use OnCollider/Trigger events since all walls are same collider and since the ball can move along a wall.