- Home /
Accessing delta-rotation values?
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.
Answer by Eno-Khaon · May 10, 2016 at 05:53 PM
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.
@$$anonymous$$o $$anonymous$$haon Thank you! I have done a workaround for the issue you have pointed out that the audio volume will not be consistent by calling this in FixedUpdate, although this is not suitable for using non-physic-based calculations.
Your answer
Follow this Question
Related Questions
Problems caused by non-unique Euler angle solutions 1 Answer
How to get Angle float of specific axis.(Turret clamping related). 2 Answers
Instantiating an object in front of the player 3 Answers
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers
Camera Zoom from rotation. 0 Answers