Created
July 5, 2014 13:01
-
-
Save k-payl/ee13faae89d4ae72470a 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 MouseOrbit : MonoBehaviour { | |
public Transform target; | |
public float distance = 10.0f; | |
public float xSpeed = 250.0f; | |
public float ySpeed = 120.0f; | |
public float yMinLimit = -20; | |
public float yMaxLimit = 80; | |
public bool NeedCruise; | |
public float CruiseSpeed = 0.5f; | |
public float x = 0.0f; | |
public float y = 0.0f; | |
void Start () | |
{ | |
var angles = transform.eulerAngles; | |
x = angles.y; | |
y = angles.x; | |
// Make the rigid body not change rotation | |
if (rigidbody) | |
rigidbody.freezeRotation = true; | |
} | |
void Update () | |
{ | |
Debug.Log("(Mouse X, Mouse Y)=(" + Input.GetAxis("Mouse X") + ", " + Input.GetAxis("Mouse Y") + ")"); | |
if (target) | |
{ | |
x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime; | |
y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime; | |
y = ClampAngle360(y, yMinLimit, yMaxLimit); | |
if (NeedCruise) | |
x += CruiseSpeed; | |
x = ClampAngle180(x); | |
transform.rotation = Quaternion.Euler(y, x, 0); | |
transform.position = (Quaternion.Euler(y, x, 0)) * new Vector3(0.0f, 0.0f, -distance) + target.position; | |
} | |
} | |
float ClampAngle180(float angle) | |
{ | |
float r = angle; | |
if (angle > 180f || angle < -180f) | |
{ | |
int d = (int) (angle/180); | |
r = angle - (d +1f)*180; | |
} | |
return r; | |
} | |
static float ClampAngle360(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