0
\$\begingroup\$

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?

.gif of issue

Code:

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);
    }
}

\$\endgroup\$
3
  • \$\begingroup\$ Looks like you're using the same y & z variables to do double-duty, tracking both a pan in progress and a rotation in progress. Have you considered giving each operation its own dedicated variables, rather than trying to share a single data container between unrelated data items this way? \$\endgroup\$
    – DMGregory
    Aug 25 '21 at 11:46
  • \$\begingroup\$ Oh wow I absolutely am.. i thought those were local variables within the method for some reason. I'll give that a shot, cheers! \$\endgroup\$
    – Rug
    Aug 25 '21 at 14:56
  • 1
    \$\begingroup\$ If that solves it for you, be sure to post your solution as an Answer below. \$\endgroup\$
    – DMGregory
    Aug 25 '21 at 15:07
0
\$\begingroup\$

Thanks to @DMGregory for his input. Adding separate vector variables for rotation and position Z,Y solved the issue.

Add explicit variables for Position and Rotation Z/Y

    private float positionZ;
    private float positionY;
    private float rotationZ;
    private float rotationY;

Use these variables instead for pan, rotate actions

    // Pan
        if (Input.GetMouseButton(1))
        {
            positionZ = Input.GetAxis("Mouse X") * moveSpeed;
            positionY = -Input.GetAxis("Mouse Y") * moveSpeed;
            
            newPosition.z += positionZ;
            newPosition.y += positionY;
        }
        
    // Rotate
        if (Input.GetMouseButton(0))
        {
            rotationZ += Input.GetAxis("Mouse X") * rotationAmount;
            rotationY += Input.GetAxis("Mouse Y") * rotationAmount;
            
            newRotation = Quaternion.Euler(0, rotationZ, rotationY);
        }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ It might be worth sharing your corrected code as a concrete example for future readers. Don't forget to mark this answer as Accepted after the required waiting period. \$\endgroup\$
    – DMGregory
    Aug 27 '21 at 16:54
  • 1
    \$\begingroup\$ Good call, cheers! \$\endgroup\$
    – Rug
    Aug 28 '21 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .