x


Rotate object with mouse cursor that slows down on release

Hi,

I am grabbing an object with the mouse cursor and rotating it, but when I release the mouse button I want it to continue to rotate a little then slow down to a stop.

I am doing the rotation in the OnMouseDrag function:

function OnMouseDrag () { RotationSpeed = 2; transform.Rotate (0, -RotationSpeed*Input.GetAxis ("Mouse X"), RotationSpeed*Input.GetAxis ("Mouse Y"), 0);

}

I have added a rigidbody component but that doesn't help. Do I need to calculate and apply a torque?

THANK YOU!

more ▼

asked Nov 17 '10 at 07:58 AM

dweeda gravatar image

dweeda
46 3 4 6

I like this question, seems like quite an interesting feature that can return in many forms!

Nov 17 '10 at 09:28 AM Proclyon
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Try this. it's untested (I don't have Unity in front of me right now)

Ok, now implemented and tested, and turns out it was a little more complex to implement - mainly because at a high framerate, you get many frames where the mouse axis input is zero during a drag - so if you don't keep track of speed over a number of frames and you release the button during one of these 'zero value' frames, it appears as though the mouse wasn't moving!

First, the rotation is done in Update rather than MouseDrag, so that it can continue after the mouse is released. I'm using OnMouseDown to detect that this object was originally clicked on, and subsequently using Input.GetMouseButton to determine that the mouse is still held down.

Basically, while dragging, it keeps a separate "average speed" value (avgSpeed), which contains the current speed of rotation averaged over the last half-second or so (Using Lerp). It then uses this value when the mouse is released as the speed to continue rotating, and gradually Lerps this value down to zero to slow the rotation.

I have also used a different method of rotating the objects so that the rotation direction is always in accordance with the direction that you're dragging the mouse in screen-space.

Hope this fits what you're looking for!

var rotationSpeed = 10.0;
var lerpSpeed = 1.0;

private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var targetSpeedX = new Vector3();

function OnMouseDown() 
{
    dragging = true;
}

function Update () 
{

    if (Input.GetMouseButton(0) && dragging) {
        speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
        avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    } else {
        if (dragging) {
            speed = avgSpeed;
            dragging = false;
        }
        var i = Time.deltaTime * lerpSpeed;
        speed = Vector3.Lerp( speed, Vector3.zero, i);   
    }

    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );

}
more ▼

answered Nov 17 '10 at 08:10 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

Without trying it out, I don't think this will work as the op asked for a solution that will continue to rotate when he releases the mouse. So doing stuff in OnMouseDrag won't work.

Nov 17 '10 at 08:21 AM StephanK

Oh yes, that should be changed to Update, good point :) (Fixed)

Nov 17 '10 at 08:28 AM duck ♦♦

Completely reworked the example due to other unexpected problems when I came to test it! (described in answer)

Nov 17 '10 at 10:00 AM duck ♦♦

@ Duck- Great code, was wondering how I could lock the rotation axis? I've tried a few things but can't get it to work. I'm going for a garage scene where you can rotate a car to see from different angles, same as in "Asphalt 5". Thanks in advance!

Dec 01 '10 at 06:54 AM DigitalDogfight

@ Duck. You're a genius. Sensational piece of code.

Dec 29 '10 at 12:27 PM dissidently
(comments are locked)
10|3000 characters needed characters left

This is exactly what I was after, however, is it much hassle to change it so that instead of click-drag I can use the arrow keys?

more ▼

answered Apr 07 '11 at 02:07 PM

user-11275 (google) gravatar image

user-11275 (google)
1

(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:

x1089
x720
x197
x154
x49

asked: Nov 17 '10 at 07:58 AM

Seen: 3734 times

Last Updated: Jul 01 '12 at 06:37 PM