Last active
August 25, 2021 06:28
-
-
Save MrMagicPenguin/c921cc435907d17aae8b45aa32eb72ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment