Last active
August 29, 2015 14:06
-
-
Save keijiro/f10664003f70054fd967 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 UnityEngine; | |
using System.Collections; | |
public class CameraOrbit : MonoBehaviour | |
{ | |
public float angularSpeed = 180; | |
public float zoomSpeed = 1; | |
public float sensibility = 6; | |
Vector2 prevMousePosition; | |
Vector2 angularVelocity; | |
float zoomVelocity; | |
void Update() | |
{ | |
var dt = Time.deltaTime; | |
var mousePosition = (Vector2)Input.mousePosition; | |
var targetAngularVelocity = Vector2.zero; | |
var targetZoomVelocity = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed; | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
prevMousePosition = mousePosition; | |
} | |
else if (Input.GetMouseButton(0)) | |
{ | |
targetAngularVelocity = (mousePosition - prevMousePosition) * (angularSpeed / (Screen.width * dt)); | |
prevMousePosition = mousePosition; | |
} | |
var exp = Mathf.Exp(-sensibility * dt); | |
angularVelocity = Vector2.Lerp(targetAngularVelocity, angularVelocity, exp); | |
zoomVelocity = Mathf.Lerp(targetZoomVelocity, zoomVelocity, exp); | |
var xPivot = transform.parent.parent; | |
var yPivot = transform.parent; | |
xPivot.localRotation = Quaternion.AngleAxis(angularVelocity.x * dt, Vector3.up ) * xPivot.localRotation; | |
yPivot.localRotation = Quaternion.AngleAxis(angularVelocity.y * dt, -Vector3.right) * yPivot.localRotation; | |
transform.localPosition += Vector3.forward * zoomVelocity * dt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment