I added a custom mouse orbit script to my project. When the right mouse button is clicked, the camera can be rotated around the player.
The issue, after this change, is my player movement has become jerky / choppy and not smooth. If I remove my mouse orbit script, player moment is back to normal i.e smooth. I can't seem to pin point what is causing this jerky movement withing MouseOrbit script
Code
/* This is where we initialize our script */
void Start()
{
Initialize();
}
/* This is where we set our private variables, check for null errors,
* and anything else that needs to be called once during startup */
void Initialize()
{
h = this.transform.eulerAngles.x;
v = this.transform.eulerAngles.y;
cameraTransform = this.transform;
cam = Camera.main;
smoothDistance = distance;
timerot = TimeSignature((1 / rotationDampening)) * 100.0f;
NullErrorCheck();
}
/* We check for null errors or warnings and notify the user to fix them */
void NullErrorCheck()
{
if (!viewTarget)
{
Debug.LogError("Please make sure to assign a view target!");
Debug.Break();
}
if (collisionLayers == 0)
{
Debug.LogWarning("Make sure to set the collision layers to the layers the camera should collide with!");
}
}
/* This is where we do all our camera updates. This is where the camera
* gets all of its functionality. From setting the position and rotation,
* to adjusting the camera to avoid geometry clipping */
void Update()
{
if (!viewTarget)
return;
if (Input.GetMouseButton(1))
{
Cursor.lockState = CursorLockMode.Locked;
h += Input.GetAxis("Mouse X") * horizontalRotationSpeed * Time.deltaTime;
v -= Input.GetAxis("Mouse Y") * verticalRotationSpeed * Time.deltaTime;
h = ClampAngle(h, -360.0f, 360.0f);
v = ClampAngle(v, minVerticalAngle, maxVerticalAngle);
// Debug.Log ("value of h: "+h);
newRotation = Quaternion.Euler(v, h, 0.0f);
}
else
{
Cursor.lockState = CursorLockMode.None;
}
if (Input.GetKeyDown(KeyCode.D))
{
h = transform.eulerAngles.y + 90;
newRotation = Quaternion.Euler(transform.eulerAngles.x, h, transform.eulerAngles.z);
}
if (Input.GetKeyDown(KeyCode.A))
{
h = transform.eulerAngles.y - 90;
newRotation = Quaternion.Euler(transform.eulerAngles.x, h, transform.eulerAngles.z);
}
/* We set the distance by moving the mouse wheel and use a custom
* growth function as the time value for linear interpolation */
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 10, minDistance, maxDistance);
smoothDistance = Mathf.Lerp(smoothDistance, distance, TimeSignature(distanceSpeed));
/*We give the rotation some smoothing for a nicer effect */
smoothRotation = Quaternion.Slerp(smoothRotation, newRotation, TimeSignature((1 / rotationDampening) * 100.0f));
newPosition = viewTarget.position;
newPosition += smoothRotation * new Vector3(0.0f, height, -smoothDistance);
/* Calls the function to adjust the camera position to avoid clipping */
CheckSphere();
smoothRotation.eulerAngles = new Vector3(smoothRotation.eulerAngles.x, smoothRotation.eulerAngles.y, 0.0f);
Vector3 dir = new Vector3(0, 0, -distance);
cameraTransform.position = newPosition;
//cameraTransform.position = viewTarget.position + smoothRotation * dir;
cameraTransform.rotation = smoothRotation;
}
Time Signature with speed as input
private float TimeSignature(float speed)
{
return 1.0f / (1.0f + 80.0f * Mathf.Exp(-speed * 0.02f));
}
return 1.0f / (1.0f + 80.0f * Mathf.Exp(-speed * 0.02f));
\$\endgroup\$