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 /
avatar image
0
Question by Rehtael · Jan 19, 2018 at 09:10 AM · c#optimizationtime.time

A more efficient way of measuring time?

I've been using the same formula over and over, and I have concerns for its efficiency.

 if (Time.time > lastFireTimeLeft && Time.time > ammoRechargeLeft && leftAmmo < 20)
 {
     Debug.Log("Left");
     //lastFireTimeLeft = Time.time + ammoDelay;
     ammoRechargeLeft = ammoRechargeRate + Time.time;
     leftAmmo = leftAmmo + 1;
 }

It's been perfectly effective to do exactly what I want, but of course, this means that the value keeps changing the longer the scene is running. I'm not super concerned for its efficiency at this time, but I don't want to shoot myself in the foot.

Comment
Add comment · Show 2
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 Harinezumi · Jan 19, 2018 at 02:00 PM 0
Share

There is no problem with the efficiency of this condition, they are just comparisons of primitive types, and they are linked with lazy boolean AND.
However, I'm not sure it does what you want it to do: Time.time tells you "the time in seconds since the start of the game" (see the scripting API). So unless you base the values of lastFireTimeLeft and ammoRechargeLeft on Time.time, they will become always true pretty fast.

avatar image Rehtael Harinezumi · Jan 22, 2018 at 12:26 AM 0
Share

It's just a small, small snippet of example coding. In playtesting, the system is working like a charm, but I had concerns for the practicality of this since the value keeps climbing and climbing the longer the scene runs.

2 Replies

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

Answer by Dray · Jan 19, 2018 at 03:23 PM

All that "wait for X seconds and then do this and that" kind of stuff can be managed pretty nicely using Coroutines!


You write something like this:

 IEnumerator YourTimedCommand() {
     SomeCommand();

     // execution will pause here and return to the same point 
     // after 0.5 Seconds, without pausing any other scripts.
     yield return new WaitForSeconds(.5f);

     SomeLateCommand();
 }

And then start it using StartCoroutine():

 StartCoroutine("YourTimedCommand");

That even works with loops and all that and as soon as the bottom of your method is reached, the coroutine stops. So you could either run an endless loop until some condition is fullfilled or just have one "async" method with some delays in it, depending on what you need, it's pretty usefull ;)

Comment
Add comment · Show 10 · 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 Rehtael · Jan 22, 2018 at 12:27 AM 0
Share

As a beginner, it'll take me a little bit to wrap my head around that, but I really appreciate the response!

avatar image Harinezumi · Jan 22, 2018 at 08:54 AM 0
Share

Indeed coroutines are a great solution for delayed calls. The only thing I would add that it is better to call your coroutine as a function ins$$anonymous$$d by name (see example below), because the string version has to find the coroutine function you want to call (adds a lot of unnecessary cost), and later it is more difficult to cancel it. So ins$$anonymous$$d of

 StartCoroutine("YourTimedCommand");

use

 StartCoroutine(YourTimedCommand());

Also, this second way you can pass typed parameters ( int, float, $$anonymous$$yCustomClass), while with the string way you can only pass an object (see scripting reference for more info).

avatar image Rehtael Harinezumi · Jan 22, 2018 at 09:11 AM 0
Share

I've been attempting a coroutine on the throttle controls, because i haven't been able to get the throttle to increase or decrease at a consistent rate, but I can't figure out a way to meld a coroutine with a Get$$anonymous$$ey. Ultimately what I'm trying to get with this one in particular is if I hold down the bumper (on my controller because mouse ai$$anonymous$$g in a flight sim is the worst ever) I want the speed to increase at a rate of 1 every 0.5s. Everything I tried so far has not worked at all. Any suggestions?

avatar image Harinezumi Rehtael · Jan 22, 2018 at 10:44 AM 0
Share

I wouldn't use a coroutine for a continuous change, I would use Update():

 // adding [SerializeField] will expose the variable in the Inspector so you can experiment with it
 [SerializeField] private float speedChangeRate = 2; // 1 unit every 0.5s is 2 units every 1s ;)
 
 [SerializeField] private string increaseSpeedButtonName; // fill with name of button
 [SerializeField] private $$anonymous$$eyCode decreaseSpeedButtonName; // fill with name of button
 
 private float speed = 0; // use the value of speed to drive physics in FixedUpdate()
 
 private void Update () {
     if (Input.GetButton(increaseSpeed$$anonymous$$ey)) { speed += speedChangeRate * Time.deltaTime; }
     if (Input.GetButton(decreaseSpeed$$anonymous$$ey)) { speed -= speedChangeRate * Time.deltaTime; }
 }

Show more comments
avatar image Dray Harinezumi · Jan 23, 2018 at 08:18 AM 1
Share

@Harinezumi yea you're totally right, that's the better way to start them and furthermore it avoids any typos. For some reason I keep using the string input version though, I don't even know why :D It's not like it looks better or anything lol

avatar image
1

Answer by ransomink · Jan 23, 2018 at 04:57 AM

I think it could be a bit simpler. I wouldn't have so many operands at once because if one condition is not true, you won't have to check the others. A simple way to measure time, you can do:

 public  float fireRate;
 private float lastShot;

 void Update()
 {
     if ( Input.GetMouseButton( 0 ) )
     {
         Shoot();
     }
 }

 void Shoot()
 {
     if ( lastShot + fireRate < Time.time )
     {
         // Pool or instantiate your bullet
         // Set whatever properties you need
         // Fire it off
         lastShot = Time.time;
     }
 }

If you're afraid of the number constantly increasing you can do it a different way.

 public  float fireRate;
 
 private float lastShot;
 private float defaultFireRate;
      
 void Awake()
 {
     defaultFireRate = fireRate;
 }
 
 void Update()
 {
     if ( fireRate <= 0 )
     {
         if ( Input.GetMouseButton( 0 ) )
         {
             Shoot();
         }
     }
     else
     {
         fireRate -= Time.deltaTime;
     }
 }
 
 void Shoot()
 {
     // Pool or instantiate your bullet
     // Set whatever properties you need
     // Fire it off
     fireRate = defaultFireRate;
 }

Coroutines are great but not the best for constantly changing values. They also generate garbage, so use them only when needed.


Personally, I believe the best option is to create Timer class and use that to set/check time for your game...

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

439 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it bad to have this many materials? 2 Answers

Alternative to KeyedByTypeCollection in Mono .Net 1 Answer

Small Timer in C#? 2 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