Skip to content

Instantly share code, notes, and snippets.

@lalamax3d
Created September 14, 2024 14:46
Show Gist options
  • Save lalamax3d/57058738d57ea084bcf1bd2b954fd570 to your computer and use it in GitHub Desktop.
Save lalamax3d/57058738d57ea084bcf1bd2b954fd570 to your computer and use it in GitHub Desktop.
Orbit Camera Controller for unity. includes PAN and Zoom
using UnityEngine;
public class CameraControl : MonoBehaviour
{
public Transform target; // The target object to orbit around
public float distance = 10.0f; // Initial distance from the target
public float zoomSpeed = 2.0f; // Speed of zooming
public float panSpeed = 0.3f; // Speed of panning
public float orbitSpeed = 5.0f; // Speed of orbiting
public bool orbitWithLMB = true; // Orbit with Left Mouse Button (LMB) if true, else Right Mouse Button (RMB)
public bool updateTargetOnClick = true; // Update orbit target on mouse click
public bool updateTargetOnPan = true; // Update orbit target on pan
private Vector3 lastMousePosition;
private float lastClickTime;
private const float doubleClickTime = 0.3f; // Time interval to detect double-click
void Update()
{
HandleMouseInput();
}
void HandleMouseInput()
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
lastMousePosition = Input.mousePosition;
}
if (Input.GetMouseButton(orbitWithLMB ? 0 : 1))
{
Orbit();
}
else if (Input.GetMouseButton(orbitWithLMB ? 1 : 0))
{
Pan();
}
Zoom();
if (updateTargetOnClick && Input.GetMouseButtonDown(0))
{
float timeSinceLastClick = Time.time - lastClickTime;
if (timeSinceLastClick <= doubleClickTime)
{
UpdateTargetOnClick();
}
lastClickTime = Time.time;
}
}
void Orbit()
{
Vector3 delta = Input.mousePosition - lastMousePosition;
float angleX = delta.x * orbitSpeed * Time.deltaTime;
float angleY = delta.y * orbitSpeed * Time.deltaTime;
transform.RotateAround(target.position, Vector3.up, angleX);
transform.RotateAround(target.position, transform.right, -angleY);
lastMousePosition = Input.mousePosition;
}
void Pan()
{
Vector3 delta = Input.mousePosition - lastMousePosition;
Vector3 panMovement = new Vector3(-delta.x * panSpeed * Time.deltaTime, -delta.y * panSpeed * Time.deltaTime, 0);
transform.Translate(panMovement, Space.Self);
if (updateTargetOnPan)
{
target.position += transform.TransformDirection(panMovement);
}
lastMousePosition = Input.mousePosition;
}
void Zoom()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
Vector3 direction = transform.position - target.position;
transform.position += direction * scroll * zoomSpeed;
}
void UpdateTargetOnClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
target.position = hit.point;
}
}
public void FitAll()
{
// Implement the logic to fit all objects in the view
// This is a placeholder and needs to be customized based on your scene
Bounds bounds = new Bounds(target.position, Vector3.zero);
Renderer[] renderers = FindObjectsOfType<Renderer>();
foreach (Renderer renderer in renderers)
{
bounds.Encapsulate(renderer.bounds);
}
float maxSize = Mathf.Max(bounds.size.x, bounds.size.y, bounds.size.z);
distance = maxSize * 1.5f;
transform.position = target.position - transform.forward * distance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment