Created
November 10, 2013 12:01
-
-
Save kuanyingchou/7397386 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 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