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 luiscmendez · Sep 14, 2012 at 08:23 PM · guibooleantime.deltatimetime.time

Delay Player From Using Action Button For A Period Of Time

I've got a script written down that works in conjunction with Time.time and a GUI progress bar. I've got it working and I can get the bar to go from 0 to 1 while holding the Action button. My issue is that once I hit the Action button again, regardless of where in the progress bar I'm at, it resets and starts back at 0.

I want to keep the player from doing anything when the Action button is held down for a period of time if the bar has reached 1. I've been told that the reason It restarts is because of how I am storing the time. Here's the code down below.

     var barDisplay : float = 0;
     var progressBarEmpty : Texture2D;
     var progressBarFull : Texture2D;
     private var barStarted : boolean = false;
     private var barStartTime : float = 0;
     private var barEndTime : float = 0;
     
     private var canDrink : boolean = false;
     private var drinkTimer : float = 15.0;
     private var isDrinking : boolean = false;
     
     function Start()
     {
         barDisplay = 0;
         barStarted = false;
         isDrinking = false;
         canDrink = true;
     }
     
     function OnGUI()
     {
         
         // draw the background:
         GUI.BeginGroup (new Rect(Screen.width / 2 - 55, 0, 110, 40));
         GUI.Box (Rect (0,0, 110, 40),progressBarEmpty);
         
         // draw the filled-in part:
         GUI.BeginGroup (new Rect (0, 0, 110 * barDisplay, 40));
         GUI.Box (Rect (0,0, 110, 40),progressBarFull);
         GUI.EndGroup ();
         
         GUI.EndGroup ();
         
     }
     
     function Update()
     {
         // the bar display is linked to the current time,
         
         if(Input.GetButton("Action"))
         {
             if( barStarted == false )
             {
                 barStarted = true;
                 barDisplay = 0;
                 barStartTime = Time.time;
             }
             else
             {
                 var barCurTime : float = Time.time;
                 barDisplay = (barCurTime - barStartTime) * 0.5;
                 barEndTime = barCurTime;
                 if(barDisplay >= 1)
                 {
                     //a result will be inserted here
                 }
             }
         }
         else
         {
             barStarted = false;
             if(barDisplay > 0)
             {
                 barCurTime = Time.time;
                 barDisplay += (barEndTime - barCurTime) * 0.0002;
                 
                 if(barDisplay <= 0)
                 {
                 //a result will be inserted here
                 }
             }
         }
     }

I wrote something that's used for something like keeping the player from shooting if a reload animation is playing for a period of time. Only problem is, I'm not even sure if It'll work, or if it's the smartest way to go about it. Here's that code below too.

 private var canDrink : boolean = false;
 private var drinkTimer : float = 15.0;
 private var isDrinking : boolean = false;
 
 function Update()
 {
     //Used to stop the Player from drinking for a period of time
     if (canDrink == false)
         {
             canDrink = true;
             drinkTimer -= Time.deltaTime;
             isDrinking = false;
         
             if(drinkTimer >= 0)
             isDrinking = true;
             canDrink = false;
             }
             else
             {
             
             if(drinkTimer <=0)
             {
             isDrinking = false;
             canDrink = true;
             }
         }
 }

If anyone could be of help it would be greatly appreciated. I've been trying to solve this for a few weeks now and no luck. Maybe there's something right in front of my face that I'm missing?

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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

How can i keep making the SendMessage repeat as long as the boolean is true 1 Answer

Game Over Screen Problem 3 Answers

I am trying to make a stamina bar and it won't drain no matter what i try. 1 Answer

increment in a value with time.deltatime/time.fixeddeltatime 1 Answer

Simple GUI Problem... 3 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