Camera teleports when panning

Hello!

So when i go to pan with the mouse i enter pan mode but gets teleported down left its always to the left but the only diffrence is the amount. Execpt the teleportation everything works great so thats always a up!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class CameraController : MonoBehaviour
{
    public InputActionAsset cameraControls;
    private InputActionMap cameraMap;
    private InputAction panAction;
    private InputAction rotateAction;
    private InputAction zoomAction;
    private InputAction mousePanAction;

    public float panSpeed = 10f;
   
    public float rotationSpeed = 5f;

    public float zoomSpeed = 1f;
    public float minZoom = 1f;
    public float maxZoom = 10f;

    private Vector3 panStartPos;
    private bool isPanning = false;
    private void Update()
    {
        Pan();
        Rotate();
        Zoom();
        MousePan();

        mousePanAction.started += _ => MousePanStarted();
        mousePanAction.canceled += _ => MousePanCan();
    }

    private void Pan()
    {
        Vector3 moveInput = panAction.ReadValue<Vector2>();

        float horInput = moveInput.x;
        float verInput = moveInput .y;

        Vector3 panDirection = transform.forward * verInput + transform.right * horInput;

        Vector3 newPosition = transform.position + panDirection * panSpeed * Time.deltaTime;
        transform.position = newPosition;
    }

    private void MousePan()
    {
        if(isPanning)
        {
            Vector3 mousePosition = Mouse.current.position.ReadValue();
            Vector3 mouseDelta = mousePosition - panStartPos;

            Vector3 panDirection = transform.forward * mouseDelta.y + transform.right * mouseDelta.x;
            Vector3 newPanPos = transform.position - panDirection * panSpeed * Time.deltaTime;
            transform.position = newPanPos;

            panStartPos = mousePosition;
        }
    }

    private void MousePanStarted()
    {
        isPanning = true;
    }

    private void MousePanCan()
    {
        isPanning = false;
        panStartPos = Mouse.current.position.ReadValue();
    }

    private void Rotate()
    {
        float rotateInput = rotateAction.ReadValue<float>();
        float rotation = rotateInput * rotationSpeed * Time.deltaTime;

        transform.Rotate(Vector3.up, rotation);
    }

    private void Zoom()
    {
        float zoomInput = zoomAction.ReadValue<float>();
        float zoom = zoomInput * zoomSpeed * Time.deltaTime;
       
        Camera mainCamera = Camera.main;

        mainCamera.fieldOfView = Mathf.Clamp(mainCamera.fieldOfView - zoom, minZoom, maxZoom);
    }

    private void OnEnable()
    {
        cameraMap = cameraControls.FindActionMap("CameraMap");
        panAction = cameraMap.FindAction("PanAction");
        rotateAction = cameraMap.FindAction("RotateAction");
        zoomAction = cameraMap.FindAction("ZoomAction");
        mousePanAction = cameraMap.FindAction("MousePanAction");

        cameraMap.Enable();
    }

    private void OnDisable()
    {
        cameraMap.Disable();
    }
}

I’m guessing you’re using stale data in your delta computations.

Bust out your Debug.Log() and prove me right or wrong!

Unity has simpler ways of getting inputs:

playerLeftRight = Input.GetAxis("Horizontal") * speed;
playerForwardBack = Input.GetAxis("Vertical") * speed;  
   
lookLeftRight = Input.GetAxis("Mouse X") * sensitivity;   
lookUpDown = Input.GetAxis("Mouse Y") * sensitivity;

As I too had issues with playing around with deltaTimes, and stayed with the above mentioned ever since. :slight_smile:

I am not completely understanding what you are doing, but if Pan() and MousePan() are supposed to do the same thing with different input, you may want to check line 57.