I’m learning cinemachine and I made a simple script for first-person. I have my virtual camera separate from the player object, with Body set to 3rd Person Follow, Aim to Do Nothing, Look At is None (Transform) and in the Follow I have my “Camera” empty game object. That empty camera game object is a child of my player object and so it’s used for the camera movement and rotation.
What I’m trying to do is rotate my player camera vertically only slightly, but smoothly and gradually whenever I either hold or press left mouse button. So holding the key or pressing it repeatedly would continue to build up the rotation. Currently it mostly works, the rotation is in small increments but the rotation is instant rather than gradual.
# Script A
public float rotationSpeed = 1f;
public float topClamp = 70.0f;
public float botClamp = -30.0f;
private Vector2 input;
private float cinemachineTargetPitch = 0.0f;
private float rotationVelocity = 0.0f;
public Transform CinemachineCameraTarget;
private float rotationAmount = 2f; # how much should the ball rotate per key press/hold
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
input = new Vector2(mouseX, mouseY);
CameraRot();
}
private void CameraRot()
{
if (input.sqrMagnitude >= 0.01f)
{
float deltaTimeMultiplier = 1f;
cinemachineTargetPitch -= input.y * rotationSpeed * deltaTimeMultiplier;
rotationVelocity = input.x * rotationSpeed * deltaTimeMultiplier;
cinemachineTargetPitch = ClampAngle(cinemachineTargetPitch, botClamp, topClamp);
CinemachineCameraTarget.localRotation = Quaternion.Euler(cinemachineTargetPitch, 0.0f, 0.0f);
transform.Rotate(Vector3.up * rotationVelocity);
}
}
private static float ClampAngle(float angle, float min, float max)
{
if (angle < -360f) angle += 360f;
if (angle > 360f) angle -= 360f;
return Mathf.Clamp(angle, min, max);
}
public void ApplyRotToBall()
{
cinemachineTargetPitch -= rotationAmount;
cinemachineTargetPitch = ClampAngle(cinemachineTargetPitch, botClamp, topClamp);
CinemachineCameraTarget.localRotation = Quaternion.Euler(cinemachineTargetPitch, 0.0f, 0.0f);
}
# Script B
# Update method
if (Input.GetMouseButton(0))
{
cameraRotation.ApplyRotToBall();
}
I used some of my old non-cinemachine camera code to get this far, plus the static ClampAngle which I found. Currently I can’t move my player which is a ball object but I can still test the rotation.