Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Wheel of fortune

Discussion in 'Scripting' started by belal, Aug 14, 2020.

  1. belal

    belal

    Joined:
    Jul 16, 2011
    Posts:
    113
    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.

    Can someone please help? I will really appreciate it.
    thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. // by @kurtdekker
    6.  
    7. public class RotateZViaDrag : MonoBehaviour
    8. {
    9.     public bool RotateAroundAxis;
    10.  
    11.     Vector3 lastPosition;
    12.  
    13.     void OnMouseDown()
    14.     {
    15.         lastPosition = Input.mousePosition;
    16.     }
    17.  
    18.     void PerformLinearRotation()
    19.     {
    20.         Vector3 currPosition = Input.mousePosition;
    21.  
    22.         Vector3 difference = currPosition - lastPosition;
    23.  
    24.         lastPosition = currPosition;
    25.  
    26.         // now choose what axis to care about... this adds X and Y
    27.         float change = difference.x + difference.y;
    28.  
    29.         // and it rotates it around the Z (forward)
    30.         transform.Rotate(new Vector3(0, 0, change));
    31.     }
    32.  
    33.     void PerformCircularRotation()
    34.     {
    35.         // where is our center on screen?
    36.         Vector3 center = Camera.main.WorldToScreenPoint(transform.position);
    37.  
    38.         // angle to previous finger
    39.         float anglePrevious = Mathf.Atan2(center.x - lastPosition.x, lastPosition.y - center.y);
    40.  
    41.         Vector3 currPosition = Input.mousePosition;
    42.  
    43.         // angle to current finger
    44.         float angleNow = Mathf.Atan2(center.x - currPosition.x, currPosition.y - center.y);
    45.  
    46.         lastPosition = currPosition;
    47.  
    48.         // how different are those angles?
    49.         float angleDelta = angleNow - anglePrevious;
    50.  
    51.         // rotate by that much
    52.         transform.Rotate(new Vector3(0, 0, angleDelta * Mathf.Rad2Deg));
    53.     }
    54.  
    55.     void OnMouseDrag()
    56.     {
    57.         if (RotateAroundAxis)
    58.         {
    59.             PerformCircularRotation();
    60.         }
    61.         else
    62.         {
    63.             PerformLinearRotation();
    64.         }
    65.     }
    66. }
     

    Attached Files:

    Zer0Cool and belal like this.
  3. belal

    belal

    Joined:
    Jul 16, 2011
    Posts:
    113
    Thank you Kurt-Dekker! I will try it out right away
     
  4. belal

    belal

    Joined:
    Jul 16, 2011
    Posts:
    113
    I tested the code out, unfortunately I was looking for something with torque force and drag.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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.
     
    belal likes this.
  6. belal

    belal

    Joined:
    Jul 16, 2011
    Posts:
    113
    Thanks!! will try it out