# Wheel of fortune

Hi everyone,
I am trying to rotate a 3d wheel of fortune type wheel by dragging on the screen. My aim is to give it a realistic look, the more force applied the longer the wheel rotates etc, but unfortunately I cant seem to figure it out, I tried multiple codes available online but mostly they are based on “click” to automatically rotate the wheel.

thanks!

Here’s a start on the actual input… see script running in enclosed package.

It has no momentum so you would have to modify it to track your own angular momentum and either increase or decrease it based on the spinny output of this code.

Kurt

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// by @kurtdekker

{
public bool RotateAroundAxis;

Vector3 lastPosition;

void OnMouseDown()
{
lastPosition = Input.mousePosition;
}

void PerformLinearRotation()
{
Vector3 currPosition = Input.mousePosition;

Vector3 difference = currPosition - lastPosition;

lastPosition = currPosition;

// now choose what axis to care about... this adds X and Y
float change = difference.x + difference.y;

// and it rotates it around the Z (forward)
transform.Rotate(new Vector3(0, 0, change));
}

void PerformCircularRotation()
{
// where is our center on screen?
Vector3 center = Camera.main.WorldToScreenPoint(transform.position);

// angle to previous finger
float anglePrevious = Mathf.Atan2(center.x - lastPosition.x, lastPosition.y - center.y);

Vector3 currPosition = Input.mousePosition;

// angle to current finger
float angleNow = Mathf.Atan2(center.x - currPosition.x, currPosition.y - center.y);

lastPosition = currPosition;

// how different are those angles?
float angleDelta = angleNow - anglePrevious;

// rotate by that much
transform.Rotate(new Vector3(0, 0, angleDelta * Mathf.Rad2Deg));
}

void OnMouseDrag()
{
if (RotateAroundAxis)
{
PerformCircularRotation();
}
else
{
PerformLinearRotation();
}
}
}
``````
2 Likes

Thank you Kurt-Dekker! I will try it out right away

I tested the code out, unfortunately I was looking for something with torque force and drag.

Steps to success using the package I posted above:

• put a rigidbody (or rigidbody2d) on the spinny thing (turn off gravity)

• instead of doing the transform.Rotate() I did, do a rigidbody.ApplyTorque

Done. Everything after that would be tuning drag and scaling multipliers.

1 Like

Thanks!! will try it out