Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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 TetchedOne · Jul 23, 2015 at 10:09 PM · 2dscreentouchscreen

2D rotating dial

I am using the new canvas system in conjunction with minor touch screen code to rotate a dial. currently the dial moves positive or negative depending on the way the user moves their mouse/finger on screen.

The client I am building this for would like to have it rotate as the user rotates their finger around the dial. My code currently only checks if they are moving up and down OR left and right. What would be simplest way to achieve their desired movement?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class SpinnerScript : MonoBehaviour {
     public Vector2 mouseDownPoint;
     public Vector2 mouseDragPoint;
     public float yDif;
     public bool dragging;
     public float rotSpeed;
 
 
     // Update is called once per fram
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             //print("thisCollider");
             //returnBtn.GetComponent<Animator> ().SetTrigger ("Show");
             dragging = true;
             mouseDownPoint = new Vector2(  Input.mousePosition.x / Screen.width , Input.mousePosition.y / Screen.height  ) ;
         }else{
             //Debug.Log("check your rays!!!");
         }
         
         if (dragging) {
             if(Input.GetMouseButton(0)){
                 mouseDragPoint = new Vector2(  Input.mousePosition.x / Screen.width , Input.mousePosition.y / Screen.height  ) ;
             }
             
             SetAnimSpeed();
             
             if (Input.GetMouseButtonUp (0)) {
                 dragging = false;
                 
             }
         }
         
     }
     
     
     void SetAnimSpeed(){
         //print ("setenter");
          yDif = 0 + ( mouseDragPoint.y - mouseDownPoint.y);
         transform.Rotate (Vector3.forward * yDif * rotSpeed);
     }
 
 }
Comment
Add comment · Show 1
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 RLin · Jul 23, 2015 at 10:25 PM 0
Share

Post your code, please

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by _Gkxd · Jul 30, 2015 at 06:16 PM

This might help you get started:

 void Update() {
     Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
     screenPosition.Scale(new Vector3(1, 1, 0));

     // Old version (Using Vector3.angle for this isn't robust)
     // float angle = Vector3.angle(Vector3.right, Input.mousePosition - transform.position);
     // Replace above line with the following:
     Vector3 difference = screenPosition - Input.mousePosition;
     float angle = Mathf.Atan2(difference.y, difference.x);

     
     transform.eulerAngles = new Vector3(0, 0, angle);
 }

This essentially makes your GameObject point at the mouse at all times. You will need to add your own offsets and conditionals.

Comment
Add comment · Show 3 · 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 TetchedOne · Jul 30, 2015 at 07:16 PM 0
Share

You sir, are a savior. Thank you so much.

avatar image _Gkxd · Jul 30, 2015 at 09:27 PM 0
Share

Hey, I just realized that the code I gave is wrong. Ins$$anonymous$$d of using Vector3.angle, I should have used $$anonymous$$athf.Atan2 ins$$anonymous$$d. Vector3.angle can't give angles greater than 180, which will cause problems.

I will edit the answer with the more robust method.

Sorry for any confusion.

avatar image TetchedOne · Aug 03, 2015 at 01:51 PM 0
Share

The newer code works great thanks again for the help!

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera setup question 2 Answers

Automatically adjusting the camera view 0 Answers

Fixing the camera's left side in a 2D Game in all screen resolutions 0 Answers

Teleporting my player to the other side of the screen? 1 Answer

2D Animation does not start 1 Answer


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