• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moverend · Dec 13, 2015 at 02:55 PM · rotationrotaterotate objectmouse drag

Rotate dial to set values by dragging on the gameobject

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!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
  
 public class DialRotate : MonoBehaviour {
    
     public List<float> rotationStop = new List<float>();
     public float sensitivity;
     public float dialValue = 0;
  
     // Update is called once per frame
     void Update () {
  
         // Set the maximun stop position to the total number of stops
         dialValue = Mathf.Clamp(dialValue, 0, rotationStop.Count -1);
  
         // Determine w$$anonymous$$ch stop is selected based on knob value from 0-1
         if (dialValue >= 0 && dialValue < 1)
             TurnDialTo(rotationStop[0]);
         if (dialValue >= 1 && dialValue < 2)
             TurnDialTo(rotationStop[1]);
         if (dialValue >= 2 && dialValue < 3)
             TurnDialTo(rotationStop[2]);
         if (dialValue >= 3 && dialValue <= 4)
             TurnDialTo(rotationStop[3]);
         if (dialValue >= 4 && dialValue <= 5)
             TurnDialTo(rotationStop[4]);
     }
  
     // Dragging on the gameobject increases or decreases dialvalue
     void OnMouseDrag ()
     {
         float dragMove = Input.GetAxis("Mouse X") * sensitivity;
         dialValue += dragMove;
     }
  
     // Rotates the knob to angle set in rotationStop
     void TurnDialTo (float position)
     {
         transform.rotation = Quaternion.Euler(0, position, 0);
     }
 }
  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by moverend · Dec 12, 2015 at 07:27 AM

Thanks to @Sose for solving t$$anonymous$$s for me. He suggested casting the float value of the rotations to an int that could be used to pick the matc$$anonymous$$ng index.

  void Update () {
    
             // Set the maximun stop position to the total number of stops
             dialValue = Mathf.Clamp(dialValue, 0, rotationStop.Count - 1);
     
             // Cast dialValue float to int and use as index
             int index = (int)dialValue;
             TurnDialTo(rotationStop[index]);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nik_2522 · Apr 17, 2016 at 12:15 PM

I want to do a similar t$$anonymous$$ng, but just that I wish to allow the user to rotate the knob in only one direction. Do you have any idea should I approach it ?

Here is the link to my question : Link

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers

How do I rotate an object when I press down a key and then it rotates back when I release the key 0 Answers

How to turn a car like in real world? 1 Answer

fixed rotation around object 1 Answer

Strange rotation pattern. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges