0

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.

bonanzaa
  • 1
  • 1

1 Answers1

0

Where is Rotate() called? Is the rotationSpeed * xInput meant to represent degrees as required?

I figure first the UpdateRotation() method was called frequently, while the Rotate() method was called to set the new rotation, in the new version you do the rotation on Rotate() are you calling it as often as you did with the UpdateRotation()?.

You could Lerp 360 degrees such as Vector3.Lerp(0, 360, t) where t should 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.

Barreto
  • 374
  • 2
  • 14