Skip to content

Instantly share code, notes, and snippets.

@dugdaniels
Created August 3, 2016 20:59
Show Gist options
  • Save dugdaniels/a56ef45c9f22ba8fdb69cc66d91d6b3e to your computer and use it in GitHub Desktop.
Save dugdaniels/a56ef45c9f22ba8fdb69cc66d91d6b3e to your computer and use it in GitHub Desktop.
The Unity MouseOrbit standard asset modified to include scroll wheel zooming.
using UnityEngine;
using System.Collections;
public class MouseOrbitAndZoom : MonoBehaviour {
public Transform target;
public float distance = 10f;
public float xSpeed = 250f;
public float ySpeed = 120f;
public float yMinLimit = -20;
public float yMaxLimit = 80;
float x = 0f;
float y = 0f;
void Start() {
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;
if (GetComponent<Rigidbody>()) {
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate() {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
distance += Input.mouseScrollDelta.y * 0.2f;
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0, 0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment