Я пытаюсь лишь слегка повернуть камеру игрока по вертикали, но плавно и постепенно, когда я удерживаю или нажимаю левую кнопку мыши. Таким образом, удерживая клавишу или нажимая ее несколько раз, вращение продолжится. В настоящее время это в основном работает, вращение происходит небольшими шагами, но вращение происходит мгновенно, а не постепенно.
Код: Выделить всё
# 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();
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ld-unity3d