I want to rotate an object like one can rotate an object in the editor.
The user performs a click on the object (anywhere). By holding the mouse button down she is then able to rotate the wheel (see image) about its x-axis. The rotation should feel like the rotation one can perform in the unity editor.
How can I achieve that? Any Help is appreciated :)!
EDIT:
The coordinates of the mouse position are given and can be used as Vector2 MousePos
.
The simplest is to use Input.GetAxis():
var sensitivity = 5;
function OnMouseDrag() {
transform.Rotate(Vector3(sensitivity * Input.GetAxis("Mouse Y"), 0, 0));
}
But you can use mousePos. I’ve use ScreenToViewportPoint() to make the code resolution indenpendent. It is not necessary. Replace with direct assignment and adjust the sensivity way down.
var sensitivity = 300.0;
private var lastPos : Vector3;
function OnMouseDown() {
lastPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
}
function OnMouseDrag() {
var pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.Rotate((pos.y - lastPos.y)*sensitivity, 0, 0);
lastPos = pos;
}