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 Qbr12 · Dec 29, 2012 at 01:14 AM · timertime.deltatimeduration

How to limit an effect to a certain duration

I'm trying to add a basic nitro boost to a car racing game. I want to give each player 5 seconds worth of nitro boost per game, which will double their speed while it's active. I'm an extreme beginner to game programming, however, and im not sure how to put what i want into code.

The basic structure of what i want is:

If C is pushed AND Nitropercent is > 0: increase speed by X decrease Nitropercent by 20*seconds active.

I know if( Input.GetKey( KeyCode.C ) ) is what i want for checking if C is pushed, transform.position += transform.forward * 8.0f * Time.deltaTime; is what i want to use to increase speed, and multiplying by Time.deltaTime is how you determine the number of seconds; but im not sure how to put it all together into a cohesive script.

Any help would be appreciated. The project isn't anything fancy, i just wanted to program a game to play with my friends that i could make as practice for using unity.

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
0
Best Answer

Answer by pad406 · Dec 29, 2012 at 03:56 AM

I'v just finished implememting something similar in my game, but I allow the user to stop using their acceleration and let it build up again. They can only start using the accelerant when their 'tank' is full.

You'll need to set up a few variables first (c#)

 private float m_tank = 0.0F;
 private float m_tank_tofill = 30.0F;  //No of seconds it takes to fill tank
 private float m_tank_usage = 10.0F;   //No of seconds it can be used for
 private bool m_accelerating;          //Are we accelearting?
 private float m_accelerateSaveSpeed;  //Used to store speed we were at before accelerating

I also have a variable MovementSpeed which holds the current speed I'm travelling at.

In FixedUpdate you'll need to add an if block

 if (m_accelerating)
 {
   m_tank -= (1F/m_tank_usage) * Time.deltatime;
   if(m_tank <= 0)
   {
      m_accelerating = false;
      m_tank = 0;
      //reset your speed here to what it was before
   }
 }
 elseif (m_tank < 1)
 {
    m_tank += (1F/M_tank_tofill) * Time.deltatime;
 }

What this will do is

  1. If you're accelerating your tank is reduced to 0 over a period of m_tank_usage seconds

  2. If you're just travelling normally your tank will fill up over a period of m_tank_tofill seconds.

Finally in Update check if your accelerate key is pressed. In my case it's CTRL and if it's pressed again the user stops accelerating. Code looks like this;

 if (Input.GetKeyDown (KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
 {
    AccelerateMe();   // A seperate function
 }

 
 void AccelerateMe()
     {
         if (m_accelerating)        // if we're accelerating then turn it off and reset speed 
         {
             MovementSpeed = m_accelerateSaveSpeed;
             m_accelerating = false;
         }
         else if (m_cur_accel >= 1)    //OK to accelerate as we have accumulated enough
         {
             m_accelerateSaveSpeed = MovementSpeed;
             MovementSpeed = MovementSpeed * 2.5F;
             m_accelerating = true;
         }
         else
         {
                   // I play a sound here to indicate that tank is not full
        }
     }


Have fun!!

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 Qbr12 · Dec 29, 2012 at 05:02 AM 0
Share

Thank you soo much. this is great.

avatar image
0

Answer by NickCh · Dec 29, 2012 at 02:09 AM

You could call the WaitForSeconds func and then deactivate the "boost" http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html

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

10 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

Related Questions

How to make this display milliseconds? 3 Answers

DeltaTime Timer wont increment when game is focused 2 Answers

Timer reset 1 Answer

Can anyone tell how can i add 2 seconds to my TIMER from another script ? 2 Answers

timer starts in the menu scene 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