Hello,
I have a turret that can rotate around its y-axis. Based on its rotation per frame (delta), it should turn up the volume of an AudioSource which is simulating the rotationsound of the turret.
Basically I need to get the delta-rotY of the turret each frame and handle its volume by this value.
I have this so far: (code snippet)
public Vector3 oldRot;
void Update(){
float deltaRotY = Vector3.Angle(tower.localRotation.eulerAngles, oldRot);
if(deltaRotY != 0)
audTower.volume = Mathf.Lerp(0, 0.15f, deltaRotY);
else audTower.volume = 0;
oldRot = tower.localRotation.eulerAngles;
}
But this will call always the else part.
You’re not quite using Vector3.Angle() correctly.
Euler angles do not represent the world-space coordinates in which the angle calculation is made. The change to make, however, will be simple.
float deltaRotY = Vector3.Angle(tower.forward, oldRot);
// ...
oldRot = tower.forward;
Get the direction which is currently “forward” for the turret.
That said, there’s still a high likelihood that the audio volume won’t be consistent. If your framerate ever changes, so will the audio volume, because you’re determining volume on a frame-by-frame basis.
If possible, it may be better to determine the rate at which the turret is turning (i.e. degrees/radians per second) and adjust the volume based on that instead. A rotation speed can be consistent with a variable framerate. The distance rotated per frame should not be.