|
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!
(comments are locked)
|
|
Try this. 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! 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)
|
|
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?
(comments are locked)
|

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