How can i make a gameObject Rotate with mouse drag

Hi all,

I am want to make a gameObject (a cube) rotate around its x,y,z axis while i press and drag the left arrow mouse down. Is this possible? Can you point out best practices or tutorials?

Thank you in advance!

Update 15/01/09

I found this c# camera component http://www.unifycommunity.com/wiki/index.php?title=MouseOrbitZoom which does exactly what i want with the difference tha I want to do it OnMouseDrag and not move the camera but the GO.

Thank you all for your answers.

I've taken a different approach and I used Quaternions.

Here is my code which drag rotates an object with easing.

var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;

function Update () {
    if(Input.GetMouseButton(0)) {
        xDeg -= Input.GetAxis("Mouse X") * speed * friction;
        yDeg += Input.GetAxis("Mouse Y") * speed * friction;
        fromRotation = transform.rotation;
        toRotation = Quaternion.Euler(yDeg,xDeg,0);
        transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);
    }
}

My only problem is that if you move the mouse very fast it bounces the object over the axis you moved it. It can be solved with using ray casting but then it will constrict the drag over the object which i don't want...

Any ideas how to prevent this bouncing?

Some quick javascript:

var cubeThing : Transform;
var lastMousePosition : Vector3;

function Update(){

    if(Input.GetMouseButton(0)){
    	if(Input.GetMouseButtonDown(0)){
    		//reset
    		lastMousePosition = Input.mousePosition;	
    	}
    	else if(Input.mousePosition.y < lastMousePosition.y){
    		cubeThing.Rotate(
              Vector3.up*(Input.mousePosition.y - lastMousePosition.y));
    	}
    	lastMousePosition = Input.mousePosition;
    }
}

It's not clear what you exactly want but the following script rotates cube around x axis when pressing left or right arrow (or use joystick or whatever is set up for left-right movement).

var rotationSpeed = 100.0;

function Update () {
    var ydir = Input.GetAxis("Horizontal");
    transform.Rotate(ydir * rotationSpeed * Time.deltaTime, 0, 0);
}

Hello,

 var cubeThing : Transform; //the object that you want rotated

 function Update(){
      if(Input.GetMouseButton(0)){
           cubeThing.Rotate(Vector3.up * Input.GetAxis("Mouse Y"));
      }
 }

Also, make sure to tag your question as answered if you found a solution.

All the best,

-Lincoln Green

A friend of mine came up with this script, this will let the rotated object ease out even if you release the mouse…

var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;

function Update () {
if(Input.GetMouseButton(0)) RotateTransform(Input.GetAxis(“Mouse X”), Input.GetAxis(“Mouse Y”));
else RotateTransform(0f, 0f);
}

function RotateTransform(xNum:float, yNum:float) {
xDeg -= xNum * speed * friction;
yDeg -= yNum * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}

a friend of mine came up with this:

var speed : int;
var friction : float;
var lerpSpeed : float;
private var xDeg : float;
private var yDeg : float;
private var fromRotation : Quaternion;
private var toRotation : Quaternion;

function Update () {
if(Input.GetMouseButton(0)) RotateTransform(Input.GetAxis(“Mouse X”), Input.GetAxis(“Mouse Y”));
else RotateTransform(0f, 0f);
}

function RotateTransform(xNum:float, yNum:float) {
xDeg -= xNum * speed * friction;
yDeg -= yNum * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}

this makes the object ease out even if the mouse button is released
credits to kvl

This is also another and not so good approach

#pragma strict
var hit:RaycastHit;
var obje:Transform;
function Update ()
{
var mPos=Input.mousePosition;
var p : Vector3 = camera.ScreenToWorldPoint (Vector3 (mPos.x,mPos.y,1));
var a : Vector3 = camera.ScreenToWorldPoint (Vector3 (Screen.width/2, Screen.height/2, 1));
if(Input.GetButton(“Fire1”))
{
print(“fire 1 basildi”);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray,hit, 200))
{
print(“ray attik ve carpti”);
if(hit.transform.gameObject.tag==“Player”)
{
//hit.transform.position=a;
if(Input.mousePosition.x>Screen.width/2){
hit.transform.Rotate(transform.right, -Time.deltaTime50);
}
if(Input.mousePosition.xScreen.height/2){
hit.transform.Rotate(transform.up, -Time.deltaTime
50);
}
}
}
}
}