Skip to content

Instantly share code, notes, and snippets.

@kuanyingchou
Created November 10, 2013 12:01
Show Gist options
  • Save kuanyingchou/7397386 to your computer and use it in GitHub Desktop.
Save kuanyingchou/7397386 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Rotator : MonoBehaviour {
private Vector3 lastMousePosition = Vector3.zero;
private bool mousePressed;
private Vector3 rotation = Vector3.zero;
private GameObject target = null;
public void Start() {
target = GameObject.CreatePrimitive(PrimitiveType.Cube);
}
public void Update () {
if(Input.GetMouseButtonDown(0)) {
mousePressed = true;
} else if(Input.GetMouseButtonUp(0)) {
mousePressed = false;
lastMousePosition = Vector3.zero;
}
if(mousePressed) {
Vector3 newPos = Input.mousePosition;
if(lastMousePosition != Vector3.zero) {
Rotate(newPos - lastMousePosition);
}
lastMousePosition = newPos;
}
}
private void Rotate(Vector3 diff) {
Debug.Log(diff);
rotation += diff;
target.transform.rotation =
Quaternion.Euler(rotation.y, -rotation.x, rotation.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment