I am working on a 3D Camera controller, where player is able to rotate the camera around an object using mouse. transform.RotateAround() is perfect for this, except for one thing - I can't figure out a way to smoothly lerp it.
For reference - this is old code, which made camera rotate smoothly, but without a specific Target, just around an arbitrary point:
private void Rotate(){
if(!EnableRotation || RotationTarget == null) return;
if(Input.GetKeyDown(KeyCode.R)) _newRotation = Quaternion.identity;
if(Input.GetKey(KeyCode.Mouse1)){
float xInput = Input.GetAxis("Mouse X");
_newRotation *= Quaternion.Euler(Vector3.up * RotationSpeed * xInput * RotationSensitivity);
}
}
private void UpdateRotation(){
transform.rotation = Quaternion.Lerp(transform.rotation,_newRotation,Time.deltaTime * RotationDampening);
}
And this, is what I have currently:
private void Rotate(){
if(!EnableRotation || RotationTarget == null) return;
if(Input.GetKeyDown(KeyCode.R)) _newRotation = Quaternion.identity;
if(Input.GetKey(KeyCode.Mouse1)){
float xInput = Input.GetAxis("Mouse X");
transform.RotateAround(RotationTarget.position,Vector3.up,RotationSpeed * xInput);
}
}
So my big question is - is there a way to lerp the rotation of transform.RotateAround()? Make it smooth. Or are there any other ways of doing this?
Thanks in advance.
I feel like I've tried everything, but no success so far.
Where is
Rotate()called? Is therotationSpeed * xInputmeant to represent degrees as required?I figure first the
UpdateRotation()method was called frequently, while theRotate()method was called to set the new rotation, in the new version you do the rotation onRotate()are you calling it as often as you did with theUpdateRotation()?.You could Lerp 360 degrees such as
Vector3.Lerp(0, 360, t)wheretshould range from 0 to 1. Or simply adjust the rotation speed accordingly, making sure you call the method enough times to have a smooth rotation.