- Home /
Spin a wheel realistically like spinning a Wheel of Fortune.
Is there a guide/tutorial/code somewhere where I can learn how to make my 2D wheel spin realistically when I touch and drag it? (On the Z Rotation Axis)
Like, the speed of its spinning will depend on how fast I touch n' dragged the wheel itself.
If I flick/rotate it very fast, the wheel would go spinning faster.
If I only moved it slowly, then it would barely spin at all. Just like what would happen if you spin a wheel like in Wheel of Fortune in real life.
Thanks!
Check out this post
You can modify this to suit your needs. Basically you store the speed that you were dragging, and use a Lerp to slow down the rotation to zero after you let go.
@flashframe How can I make it so that it only moves my wheel on the Z Axis? Since my 2D Wheel spins on the Z Axis only.
The code on that answer there works, but it rotates my object on all XYZ Axis. Thanks.
You could remove
transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
and add:
transform.Rotate( Camera.main.transform.forward * speed.x * rotationSpeed, Space.World );
That would use horizontal movement to spin on the z-axis only. Alternatively, check out @Bored$$anonymous$$ormon 's answer below. $$anonymous$$ight suit your needs better.
I'm not quite sure what to do with what Bored$$anonymous$$ormon said. How do I add torque and what should I do after that? How can I make it move?
This is all very confusing for me even after I watch some tutorials.
Answer by florianveltman · Apr 01, 2015 at 02:07 PM
I would use a HingeJoint2D. Add the component to your wheel and set its anchor to the center of it. You can then use any rigidbody dragging script you like to move it at the speed you want.
Take a look at this video tutorial for a clearer idea on how hinge joints work. If you do not have any rigidbody dragging code yet, maybe take a look at the sample assets, filed under "2D" in the Import Assets menu.
Hi, I know this is a bit old but I'm new to Unity and struggling with the exact same issue. Can anybody go into a bit more detail on how to implement the HingeJoint2D solution? Thanks!
Answer by Safforn · Mar 30, 2015 at 09:50 AM
No tutorial, but you can measure the distance and time from a touch. If you know those two you can find out the speed. Then you can use Quaternion to spin the wheel with the speed you found out.
Answer by Kiwasi · Apr 01, 2015 at 10:44 AM
Just constrain the centre of the object with a joint and then use add torque. Problem solved.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191633im_/https://answers.unity.com/themes/thub/images/avi.jpg)