Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by GKdev1980 · Nov 29, 2017 at 10:32 PM · buttontrigger

Continuous action while pressing UI Button

I'll start by saying that I eventually accomplished what I wanted to do, but with quite a bit of a hassle and code. I'm wondering if there's another more efficient way.

What I want to do is quite simple. I have a UI Button and while I keep it pressed I want an object to rotate (actually 2 buttons, one for each direction). I have a function handling the rotation. The embedded OnClick method doesn't seem to do the trick, as it calls the method once and not for as long as I press the key.

I did some digging around and added an Event Trigger component to my button. Someone suggested I should add an UpdateSelected event and call my method, but this didn't work either, cause the method did start as soon as I pressed the button, but didn't stop when I released the button. I don't know if there's a fix for that.

I went on a different route, added a PointerDown and a PointerUp event, hoping I could control the rotation through a boolean variable (start the rotation when the boolean is true and stop when it's false). But that didn't seem to work either, as I didn't seem to have access to the boolean through the event trigger (setting them to static didn't help), leading me to create additional methods just to change the value of the boolean variable.

I ended up with this:

     bool rotateCCW;
     bool rotateCW;
 
     public float speed = 100;
 
     // Use this for initialization
     void Start () {
         fObjTransform = fProduct.GetComponent<Transform> ();
 
         rotateCCW = false;
         rotateCW = false;
     }
     
     // Update is called once per frame
     void Update () 
     {
     
         if (rotateCCW) {
             RotateCounterClockWise ();
         } else if (rotateCW) 
         {
             RotateClockWise ();
         }
 
     }
 
     public void StartRotateCCW()
     {
         rotateCCW = true;
     }
 
     public void StopRotateCCW()
     {
         rotateCCW = false;
     }
 
     public void StartRotateCW()
     {
         rotateCW = true;
     }
 
     public void StopRotateCW()
     {
         rotateCW = false;
     }
 
     public void RotateClockWise()
     {
         fObjTransform.Rotate (Vector3.up * speed * Time.deltaTime);
     }
 
     public void RotateCounterClockWise()
     {
         fObjTransform.Rotate (Vector3.down * speed * Time.deltaTime);
     }
 
 }


alt text alt text

Isn't there a shorter way? Am I doing something wrong?

rotatescreenshot01.jpg (30.6 kB)
rotatescreenshot02.jpg (29.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by giusti · Nov 11, 2019 at 04:46 PM

This was helpful, thanks.

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

145 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 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 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 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 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 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 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

How to get "on trigger enter"( or exit) to only react to a specific tag 1 Answer

how to separate differents triggers 1 Answer

Instantiate a canvas with a button when trigger 0 Answers

clicking button using script? 1 Answer

[resolved] How do I deactivate a button once its height is < 40 in c# 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