Skip to content

Instantly share code, notes, and snippets.

@3dln
Last active April 24, 2024 16:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3dln/c16d000b174f7ccf6df9a1cb0cef7f80 to your computer and use it in GitHub Desktop.
Save 3dln/c16d000b174f7ccf6df9a1cb0cef7f80 to your computer and use it in GitHub Desktop.
A simple Unity C# script for orbital movement around a target gameobject
// A simple Unity C# script for orbital movement around a target gameobject
// Author: Ashkan Ashtiani
// Gist on Github: https://gist.github.com/3dln/c16d000b174f7ccf6df9a1cb0cef7f80
using System;
using UnityEngine;
namespace TDLN.CameraControllers
{
public class CameraOrbit : MonoBehaviour
{
public GameObject target;
public float distance = 10.0f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20;
public float yMaxLimit = 80;
float x = 0.0f;
float y = 0.0f;
void Start()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
float prevDistance;
void LateUpdate()
{
if (distance < 2) distance = 2;
distance -= Input.GetAxis("Mouse ScrollWheel") * 2;
if (target && (Input.GetMouseButton(0) || Input.GetMouseButton(1)))
{
var pos = Input.mousePosition;
var dpiScale = 1f;
if (Screen.dpi < 1) dpiScale = 1;
if (Screen.dpi < 200) dpiScale = 1;
else dpiScale = Screen.dpi / 200f;
if (pos.x < 380 * dpiScale && Screen.height - pos.y < 250 * dpiScale) return;
// comment out these two lines if you don't want to hide mouse curser or you have a UI button
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
transform.rotation = rotation;
transform.position = position;
}
else
{
// comment out these two lines if you don't want to hide mouse curser or you have a UI button
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
if (Math.Abs(prevDistance - distance) > 0.001f)
{
prevDistance = distance;
var rot = Quaternion.Euler(y, x, 0);
var po = rot * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
transform.rotation = rot;
transform.position = po;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
}
@3dln
Copy link
Author

3dln commented Sep 19, 2018

Put the script on your camera and specify a target

@omnidream
Copy link

Thank you kind sir, works like a charm!

@PiedraViviente
Copy link

Nice, this is exactly what I was looking for.

@ChristopherHaws
Copy link

ChristopherHaws commented May 25, 2022

Not sure but should this:

                if (Screen.dpi < 1) dpiScale = 1;
                if (Screen.dpi < 200) dpiScale = 1;
                else dpiScale = Screen.dpi / 200f;

be this?

                if (Screen.dpi < 1) dpiScale = 1;
                else if (Screen.dpi >= 200) dpiScale = 1;
                else dpiScale = Screen.dpi / 200f;

@MrBlackMedia
Copy link

Thanks so much for this 3dIn!
I have a quick question...
I'm still learning coding so I wasn't able to see the answer as quick as you would. I need to clamp the distance the camera can zoom out from the scroll wheel. Something like just 8m. Maybe some kind of return.mathfClamp? Not sure where it would go or be implemented. Any thoughts here would be greatly appreciated!
Cheers
Jeff

@Snow-Okami
Copy link

Snow-Okami commented Aug 16, 2022

Thanks so much for this 3dIn! I have a quick question... I'm still learning coding so I wasn't able to see the answer as quick as you would. I need to clamp the distance the camera can zoom out from the scroll wheel. Something like just 8m. Maybe some kind of return.mathfClamp? Not sure where it would go or be implemented. Any thoughts here would be greatly appreciated! Cheers Jeff

Add 2 variables (as well as a step per scroll) and switch the placement so you don't get the camera bumping when scrolling:

distance -= Input.GetAxis("Mouse ScrollWheel") * distanceStep; if (distance < minDistance) distance = minDistance; if (distance > maxDistance) distance = maxDistance;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment