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