r/Unity2D•
idosc

Rotate a dial and stay at end-position

I'm trying to rotate a 2D circle with circular swipes/mouse drags so that the mouse drag will change its rotation relatively to its original rotation (rather than snap into the mouse position), and also it will stay at its new final rotation once the mouse click is lifted up.

After lots of searches and attempts this is the closest solution I've come across: http://answers.unity3d.com/questions/524687/rotate-a-dial-with-circular-motions.html

However, in this solution, the dial/circle snaps back into its original rotation when the click is lifted up. I'd like it to stay there and continue from that new rotation the next time I drag it around. I tried to save the rotation given by InputIsHeld() and apply it whenever the GetKey.Input(KeyCode.Mouse0) is false, but for some reason it still snaps back.

Thanks for your help and guidance!

Posted Jan 16, 2017, 12:03 PM UTC

23
Giacomand•

Variables you will want are:

  • Current Angle
  • Angle Offset

What you need to do is to record the angle offset, from when the player first clicks the mouse button, and then add that in all turning calculations.

You would also need to keep track of it's current angle after it has been moved. Then when you calculate a new offset, if the player decides to turn it again, you determine the difference from the current angle.

1
idoscOP•

You would also need to keep track of it's current angle after it has been moved. Then when you calculate a new offset, if the player decides to turn it again, you determine the difference from the current angle.

I'm having problems with that part. I added a Quaternion variable (let's call it X) and basically wrote:

x = transform.rotation;

At the end of InButtonHeld(). But for some reason the value of x reverts to the original position whenever I let the button go.

1
Giacomand•

It's hard to say without seeing the source code. Could you post it to pastebin?

1