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
-2
Question by samz · Apr 29, 2012 at 10:00 PM · buttonwaitforseconds

wait for 3 seconds button??

im looking to script for a button.

when i press it it does somethink, but when i hold it for 3 seconds it does something else ??

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 HHameline · Apr 29, 2012 at 10:26 PM 1
Share

Are you using unity's OnGUI function to make your buttons? or are you using like GUITextures, or 3D assets that you raycast too? Hard to help you with so little information :/ do you have you starting code at all?

3 Replies

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

Answer by psychentist · Apr 30, 2012 at 01:43 AM

Ok. Normally I'd tell you to learn your own scripting, but I love coding so much, I couldn't pass up this uber easy question. For a 3d asset such as a cube you want to click, here's the script.

 private var i : int;
 
 function OnMouseDown(){
    StartCount();
 }
 
 function OnMouseUp(){
    StopCount();
 }
 
 function StartCount(){
    for (i=0;i>-1;i++){
       yield WaitForSeconds(1);
                                }
 }
 
 function StopCount(){

    if (i>3){
       //Do your greater than 3 seconds of holding stuff here.
               }
 
    else{
       //Do your less than 3 seconds stuff here.
          }

    i = 0;

}

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 psychentist · Apr 30, 2012 at 01:47 AM 0
Share

As for a GUI button, It's quite the same, but you gotta declare the button's domain and such. See the scripting reference for everything else you need to know.

avatar image
0

Answer by kolban · Apr 30, 2012 at 02:49 AM

For determining how long a mouse button is held down, I think the following code may work:

 private var mouseDownStart:float;
 private var hasFired:boolean = false;
 function Update () {
     if (Input.GetMouseButtonDown(0))
     {
         hasFired = false;
         mouseDownStart = Time.time;
     }
     if (Input.GetMouseButton(0))
     {
         if (!hasFired && (Time.time - mouseDownStart) >= 3)
         {
             // 3 seconds has passed with the mouse down
             hasFired=true;
             // Do something
         }
     }
 }

The way this works is to note the time the mouse button was first held down and then, while it is still down, see if it has been 3 seconds since the mouse button was first held down. The guard prevents re-fires after 3 seconds.

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 samz · Apr 30, 2012 at 07:57 AM

its using GUI textures...i forgot to say This is the script for the touch button copied from the SideScroll tutorial n this is what i had so far:

 function Start() {
 
     endTime = 0;
 
 }
 
  
 
 function Update() {
 
 //:::::::::::::::::::::::::  H I T  ::::::::::::::::::::::::::::::      
 
     if (!hitTouchPad.IsFingerDown()) {
 
        canHit = true;
 
     }
 
  
 
     if (canHit && hitTouchPad.IsFingerDown()) {
 
        while( hitTouchPad.IsFingerDown() ){  
 
          if(endTime < 3){
 
           endTime += Time.deltaTime;
 
          }else{
 
                  ShowItems();
 
                  break;
 
             }
 
          }
 
          if( endTime < 3 ){
 
                 hit = true;
 
            canHit = false;   
 
          } 
 
     }
 
  
 
     if ( hit ) {
 
        hit = false;
 
        //hit code
 
     }
 
 }
 
  
 
 function ShowItems(){
 
     //show other hit items
 
 }
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

7 People are following this question.

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

Related Questions

How do I make a program that first press button 1 release it and then press button 2 in order to walk? 0 Answers

Putting a delay in a button OnClick ? (Javascript) 3 Answers

I have a button with a cooldown. How do I make it so the cooldown time increases with each press of the button? 2 Answers

Best way to delay the GameOver screen? 0 Answers

How To Make UI Button 2 Appear Three Seconds After UI Button 1 is pressed 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