I'm working on a generic camera rig using mouse inputs, kind of like an RTS camera but it will be used to look at models with as a sort of turntable viewer.
The issue I am having is that when I rotate the camera, then pan, then try to rotate again, the rotation resets to the initial value. I think the issue is that the old rotation values aren't remembered when I begin the new rotate operation, so it resets to the default value (0,0,0,1).
If this is indeed the issue, how should I approach saving the value at the end of the frame, then reuse it in following frames?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRigController : MonoBehaviour
{
public float moveSpeed;
public float moveTime;
public float rotationAmount;
public float zoomAmount;
public Transform cameraPosition;
public Quaternion newRotation;
public Vector3 newPosition;
public Vector3 newZoom;
// mouse rotato
public Vector3 rotateStartPosition;
public Quaternion rotateCurrentPosition;
private float z;
private float y;
void Start()
{
newPosition = transform.position;
newRotation = transform.rotation;
newZoom = cameraPosition.localPosition;
}
void LateUpdate()
{
HandleMovementInput();
}
void HandleMovementInput()
{
// Zoom
if (Input.mouseScrollDelta.y !=0)
{
newZoom.x += Input.mouseScrollDelta.y * zoomAmount;
}
// Pan
if (Input.GetMouseButton(1))
{
z = Input.GetAxis("Mouse X") * moveSpeed;
y = -Input.GetAxis("Mouse Y") * moveSpeed;
newPosition.z += z;
newPosition.y += y;
}
// Rotate
if (Input.GetMouseButton(0))
{
z += Input.GetAxis("Mouse X") * rotationAmount;
y += Input.GetAxis("Mouse Y") * rotationAmount;
newRotation = Quaternion.Euler(0,z,y);
rotateCurrentPosition = newRotation;
}
transform.position = newPosition;
transform.rotation = newRotation;
cameraPosition.localPosition = Vector3.Lerp(cameraPosition.localPosition, newZoom, Time.deltaTime * moveTime);
}
}