x


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.

more ▼

asked Jan 14 '10 at 09:34 PM

chchrist gravatar image

chchrist
116 5 7 14

(comments are locked)
10|3000 characters needed characters left

7 answers: sort voted first

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?

more ▼

answered Mar 20 '10 at 11:08 AM

chchrist gravatar image

chchrist
116 5 7 14

Thanks.. I am looking for it too... :)

Mar 09 '11 at 07:32 AM Thet Naing Swe

Thanks, I was also looking for something like this ^^

Jun 25 '11 at 06:56 AM Aer93

can't you scale up the collision box on the object to fill the screen? this way you are always touching the object and you can use ray cast.

Feb 01 '12 at 11:55 AM vengeance92
(comments are locked)
10|3000 characters needed characters left

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;
    }
}
more ▼

answered Jan 15 '10 at 12:49 AM

Brian Kehrer gravatar image

Brian Kehrer
2.8k 9 11 50

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jan 16 '10 at 02:57 PM

flaminghairball gravatar image

flaminghairball
928 4 9 21

(comments are locked)
10|3000 characters needed characters left

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.deltaTime*50); } if(Input.mousePosition.xScreen.height/2){ hit.transform.Rotate(transform.up, -Time.deltaTime*50); } }
} } }

more ▼

answered Oct 12 '12 at 05:00 PM

cengizhangokben gravatar image

cengizhangokben
0 1 1

(comments are locked)
10|3000 characters needed characters left

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);
}
more ▼

answered Jan 15 '10 at 12:51 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

I guess Brian did better understand what you want. I see now that you probably meant 'left mouse button' instead of 'left arrow'.

Jan 15 '10 at 12:54 AM Jaap Kreijkamp

Sorry I meant mouse yes...

Jan 15 '10 at 09:20 PM chchrist
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2168
x983

asked: Jan 14 '10 at 09:34 PM

Seen: 18237 times

Last Updated: Oct 12 '12 at 05:00 PM