Skip to content

Instantly share code, notes, and snippets.

@SteveSwink
Created February 19, 2013 22:58
Show Gist options
  • Save SteveSwink/4991023 to your computer and use it in GitHub Desktop.
Save SteveSwink/4991023 to your computer and use it in GitHub Desktop.
Click and drag to rotate
using UnityEngine;
using System.Collections;
public class MouseOrbitClickAndDrag : MonoBehaviour {
public Transform target;
public float distance = 5.0f;
public float xAccel = 0.3f;
public float maxXSpeed = 0.5f;
public float yAccel = 0.3f;
public float maxYSpeed = 0.5f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = 0.5f;
public float distanceMax = 15f;
public float mapRotationDamp = 0.75f;
public float scrollSpeed = 5.0f;
float x = 0.0f;
float y = 0.0f;
bool doOrbit = false;
Vector3 lastPos = Vector3.zero;
Vector3 mouseDelta = Vector3.zero;
// Use this for initialization
void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// store the first mouse position
lastPos = Input.mousePosition;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void Update(){
if(Input.GetMouseButton(0)){
doOrbit = true;
// store the change in mouse input
mouseDelta = Input.mousePosition - lastPos;
mouseDelta.x = Mathf.Clamp(mouseDelta.x , -maxXSpeed, maxXSpeed);
if(mouseDelta.x > maxXSpeed) mouseDelta.x = maxXSpeed;
if(mouseDelta.x < -maxXSpeed) mouseDelta.x = -maxXSpeed;
mouseDelta.y = Mathf.Clamp(mouseDelta.y , -maxXSpeed, maxYSpeed);
if(mouseDelta.y > maxYSpeed) mouseDelta.y = maxYSpeed;
if(mouseDelta.y < -maxYSpeed) mouseDelta.y = -maxYSpeed;
}else{
// zero out mouse delta
lastPos = Input.mousePosition;
if(mouseDelta.magnitude > 0.1f){
mouseDelta *= mapRotationDamp;
}else{
mouseDelta = Vector3.zero;
}
doOrbit = false;
}
}
void LateUpdate () {
if (target) {
// set the x and y euler angles; these are cumulative so they become big numbers (-1400 etct)
x += mouseDelta.x * xAccel * Time.deltaTime * 1f; // distance *
y -= mouseDelta.y * yAccel * Time.deltaTime * 1f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
if(y > yMaxLimit) y=yMaxLimit;
if(y < yMinLimit) y=yMinLimit;
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * scrollSpeed, distanceMin, distanceMax);
// not sure what this was doing - subtracting distance on ray hit?
// RaycastHit hit;
// if (Physics.Linecast (target.position, transform.position, out hit)) {
// distance -= hit.distance;
// }
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.position = position;
transform.rotation = rotation;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
@jBachalo
Copy link

this is great. One issue however I've noticed is the movement seems to 'stutter' at regular intervals...

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