Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Eugenius · May 19, 2015 at 09:56 PM · uiinputunity5rotate

[Unity5] Rotate UI button with mouse/finger

Hi everyone,

It's been a while since I asked last but I'm in a bit of a pickle.

I'm wondering if there's any easy way of rotating a new UI button with the mouse/tap gesture.

I've made up a script based on another answer found on the forum but it doesn't seem to work - specifically because Input.GetMouseButtonDown(0) is true only upon click and not while the mouse button is actually down.

This is the script so far:

 public class RotateScript : MonoBehaviour {
 
     private float rotationSpeed = 10.0f;
     private float lerpSpeed = 1.0f;
 
     private Vector3 theSpeed;
     private Vector3 avgSpeed;
     public bool isDragging = false;
     private Vector3 targetSpeedX;
 
     public Transform myObj;
 
     public void OnMouseDown()
     {
         isDragging = true;
     }
     
     // Update is called once per frame
      void Update () 
     {
 
         if (isDragging && Input.GetMouseButtonDown(0)) 
         {
             theSpeed = new Vector3 (-Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"),0.0f);
             avgSpeed = Vector3.Lerp (avgSpeed, theSpeed, Time.deltaTime * 5);
         } else 
         {
             if(isDragging)
             {
                 theSpeed = avgSpeed;
                 isDragging = false;
             }
 
             float i = Time.deltaTime * lerpSpeed;
             theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
         }
 
         myObj = this.GetComponent<Transform> ();
 
         transform.Rotate (myObj.up * theSpeed.x * rotationSpeed, Space.World);
         transform.Rotate (myObj.right * theSpeed.x * rotationSpeed, Space.World);
     }
 }
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
1
Best Answer

Answer by RabidCabbage · May 19, 2015 at 10:20 PM

Have you tried Input.GetMouseButton() ?

This constantly sends, instead of just once.

Comment
Add comment · Show 1 · 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 Eugenius · May 21, 2015 at 08:50 PM 0
Share

It seems to send exactly the same as Get$$anonymous$$ouseButtonDown. I fixed it by adding an extra bool but am marking this as right answer since no other has been added. Thanks!

avatar image
0

Answer by ChosenVan · Nov 04, 2018 at 12:53 PM

@Eugenius thank you :) I took most part of your codes.

this part works for me :)

 public class ButtonRotate : MonoBehaviour {
 
     private float rotationSpeed = 10.0f;
 
     private Vector3 theSpeed;
     private Vector3 avgSpeed;
 
     public Transform myObj;
 
     void Update () 
     {
 
         if (Input.GetMouseButton(0)) 
         {
             theSpeed = new Vector3 (-Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"));
             avgSpeed = Vector3.Lerp (avgSpeed, theSpeed, Time.deltaTime * 5);
         } else 
         {
             theSpeed = new Vector3 (0,0,0);
         }
 
         myObj = this.GetComponent<Transform> ();
         transform.Rotate (myObj.forward * theSpeed.x * rotationSpeed, Space.World);
     }
 }
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.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D rotation around hinge joint with joystick input 0 Answers

Based on touch rotate camera object 0 Answers

Space Camera Issue. 1 Answer

Unity 5 UI Mask and Android (Tegra 4) 1 Answer

How to alter input to continuous rather than tap? 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