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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Rotate dial to set values by dragging on the gameobject

Discussion in 'Scripting' started by Assembly4D, Dec 11, 2015.

  1. Assembly4D

    Assembly4D

    Joined:
    Nov 21, 2015
    Posts:
    3
    Hi, I'm setting up a script to attach to a dial gameObject on a control panel. When the user clicks and drags over the dial, the dial rotates and snaps into predetermined positions.

    Once the script is attached to a dial, I set the number of stop positions then enter the angles for each of those stops in a list.

    The script I have is working fine, but I'm just wondering how to better code the messy IF statements I'm using. It works great for up to 5 preset positions.....but I want it to be able to work on dials requiring any number of stops.

    Any ideas? Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class DialRotate : MonoBehaviour {
    6.    
    7.     public List<float> rotationStop = new List<float>();
    8.     public float sensitivity;
    9.     public float dialValue = 0;
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.         // Set the maximun stop position to the total number of stops
    15.         dialValue = Mathf.Clamp(dialValue, 0, rotationStop.Count -1);
    16.  
    17.         // Determine which stop is selected based on knob value from 0-1
    18.         if (dialValue >= 0 && dialValue < 1)
    19.             TurnDialTo(rotationStop[0]);
    20.         if (dialValue >= 1 && dialValue < 2)
    21.             TurnDialTo(rotationStop[1]);
    22.         if (dialValue >= 2 && dialValue < 3)
    23.             TurnDialTo(rotationStop[2]);
    24.         if (dialValue >= 3 && dialValue <= 4)
    25.             TurnDialTo(rotationStop[3]);
    26.         if (dialValue >= 4 && dialValue <= 5)
    27.             TurnDialTo(rotationStop[4]);
    28.     }
    29.  
    30.     // Dragging on the gameobject increases or decreases dialvalue
    31.     void OnMouseDrag ()
    32.     {
    33.         float dragMove = Input.GetAxis("Mouse X") * sensitivity;
    34.         dialValue += dragMove;
    35.     }
    36.  
    37.     // Rotates the knob to angle set in rotationStop
    38.     void TurnDialTo (float position)
    39.     {
    40.         transform.rotation = Quaternion.Euler(0, position, 0);
    41.     }
    42. }
    43.  
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Your if statements are basically a function that converts float values from 0..1 -> 0 and 1..2 -> 1 and so forth. You can simply cast your float (dialValue) to an integer to achieve the same result.

    Code (csharp):
    1.  
    2. int index = (int) dialValue;
    3. TurnDialTo(rotationStop[index]);
    4.  
     
  3. Assembly4D

    Assembly4D

    Joined:
    Nov 21, 2015
    Posts:
    3
    Works like a charm! Did not know you could do that. Nice and clean. Thank you!