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
3
Question by oliver-jones · Apr 04, 2011 at 07:51 PM · coroutinetimeyieldinvokerepeatingdecrease

Increase/Decrease Value With Time - Problem

Okay, I thought I would make a new post with my problem. I'm basically trying to make a flame thrower turret that, when fires decreases its fuel, and when the fuel is empty it then refuels back to 100 - and then continues to fire.

The problem I'm having is increasing and decreasing the fuel, mainly to do with time, and frame rate. In the editor, its slow, and in the build, its really fast - and from that, I'm assuming its a frame rate problem??

Here are bits of my flame controller script:

var flameDamage : float = 0.6; private var externalSmooth : SmoothLookAt; private var particleDamage : FlameDamage;

//private var flameRange : float; private var flameOn : boolean = false; var flameParticle : ParticleEmitter;

var flameSpin : boolean = false; //var flameSpinInt : int = 720; private var speed : int = 200;

var flameAudio_start : AudioClip; var flameAudio_middle : AudioClip; var flameAudio_end : AudioClip;

// Cache externalSmooth externalSmooth = gameObject.GetComponentInChildren(SmoothLookAt);

private var audioOnce : boolean = true; private var audioOnce_end : boolean = true; var coolingBar : Transform; private var maxBar : float = 0.2;

var maxFuel : float = 100; var currentFuel : float = 100; var fillSpeed : int = 100; var emptySpeed : float = 10;

private var timer : float = 0.0f;

private var fuelEmpty : boolean = false;

function Awake(){ flameParticle.emit = false;

}

function Update () { // fixed update // Set up particleDamage \\ particleDamage = flameParticle.GetComponent(FlameDamage); if (!particleDamage) particleDamage = flameParticle.gameObject.AddComponent(FlameDamage);
particleDamage.damage = flameDamage;

 if(externalSmooth.target){
     flameOn = true;
     flameParticle.emit = true;
 }
 else{
     flameOn = false;
     flameParticle.emit = false;
 }
 // Spinning FlameThrower \\
 if(flameSpin == true){
     externalSmooth.TurretActive = false;
     Spin();
 }

 // fuel is full - continue to fire 
 if(fuelEmpty == false && flameSpin == false){
     externalSmooth.TurretActive = true;
     flameOn = true;
 }
 // fuel is empty - stop firing and refuel
 else if(fuelEmpty == true){
     externalSmooth.TurretActive = false;
     flameOn = false;
     flameParticle.emit = false;
     if(currentFuel < maxFuel){
         InvokeRepeating("IncreaseFuel", 0, fillSpeed);
         //currentFuel += Time.deltaTime * fillSpeed;
     }
     if(currentFuel == maxFuel){
         fuelEmpty = false;
     }
 }
 // incase current goes above max in refuel - reset to 100
 if(currentFuel > maxFuel){
     currentFuel = maxFuel;
 }

 // flame particle functions - ON
 if(flameParticle.emit == true){
     if(audioOnce == true){
         FlameAudio();
         audioOnce = false;
     }
     timer = 0;
     if(currentFuel > 0){
         InvokeRepeating("DecreaseFuel", 0, emptySpeed);
         //currentFuel -= (Time.deltaTime * emptySpeed);
         //currentFuel --;
     }
 }

 // check weather fuel is empty
 if(currentFuel <= 0){
     fuelEmpty = true;
 }

 // flame particle function - OFF \\
 if(flameParticle.emit == false){
     timer += Time.deltaTime;
     if(timer > 0.4){
         if(audioOnce_end == true){
             FlameAudioEnd();
             audioOnce_end = false;
         }
     }
 }   

 // Cooling health bar \\
 var CameraPosition = Camera.main.transform.position;
 coolingBar.transform.rotation = Camera.main.transform.rotation;
 coolingBar.transform.Rotate(-90, 0, 0);
 coolingBar.transform.renderer.material.color = Color.cyan;
 coolingBar.transform.localScale.x = (maxBar) * (currentFuel/maxFuel); //(MaxBar) * (Health/MaxHealth); ---- 0.2 * 15/15

}

function DecreaseFuel(){ currentFuel --; }

function IncreaseFuel(){ currentFuel ++; }

I would appreciate any help from this, and I apologies for repeating a post - but this is really bugging me. Cheers all!

Comment
Add comment · Show 3
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 oliver-jones · Apr 04, 2011 at 07:52 PM 0
Share

Forgot to add: I want the fuel to decrease with time

avatar image Parthon · Apr 04, 2011 at 08:14 PM 0
Share

What was wrong with the lines that used Time.deltaTime?

avatar image oliver-jones · Apr 04, 2011 at 08:33 PM 0
Share

Its still realllyyy fast in the build

4 Replies

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

Answer by Eric5h5 · Apr 26, 2011 at 08:31 AM

I would highly recommend using coroutines for this...using Update in that manner is kind of torturous. ;) Update is for stuff that happens every frame, but you don't want stuff to happen every frame, only sometimes. In any case I'd guess the root problem is that you're launching new instances of InvokeRepeating every frame, since at first look there's nothing preventing that from happening.

Here's a simplified example that just uses user input to decrease a fuel variable and then refills it when it runs out, but it should illustrate the general concept (switch the inspector to debug mode to see the status of the currentFuel variable at runtime, or else make it a public variable):

var maxFuel = 100.0; var fuelUsageRate = 10.0; var fuelRefillRate = 100.0; private var currentFuel : float;

function Awake () { currentFuel = maxFuel; FireControl(); }

function FireControl () { while (true) { yield WaitForInput(); yield Fire(); if (currentFuel <= 0.0) { yield Reload(); } } }

function WaitForInput () { while (!Input.GetButton("Fire1")) { yield; } }

function Fire () { while (Input.GetButton("Fire1") && currentFuel > 0.0) { currentFuel -= fuelUsageRate * Time.deltaTime; yield; } }

function Reload () { while (currentFuel < maxFuel) { currentFuel += fuelRefillRate * Time.deltaTime; yield; } currentFuel = maxFuel; }

Comment
Add comment · Show 4 · 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 Jean-Fabre · Apr 28, 2011 at 07:12 AM 0
Share

grand master :) any chance you could do that in cSharp as well, I have difficulties doing all these coroutines stuff in c# for some reason, I miss something... thanks :) and that script is a perfect example.

avatar image Eric5h5 · Apr 28, 2011 at 08:05 AM 1
Share

@$$anonymous$$ Fabre: Well, one of the main reasons I usually use JS is that C# is quite annoying when it comes to coroutines. 1) $$anonymous$$ake sure coroutines return type IEnumerator. 2) Use "`yield return StartCoroutine($$anonymous$$yFunction())`" ins$$anonymous$$d of "`yield $$anonymous$$yFunction()`". 3) Use "`yield return null`" ins$$anonymous$$d of "`yield`". 4) $$anonymous$$aybe something else I forgot. ;)

avatar image Eric5h5 · Apr 28, 2011 at 08:10 AM 1
Share

Oh yeah: 4) In Awake, do "`StartCoroutine(FireControl())`" ins$$anonymous$$d of "`FireControl()`".

avatar image Ippokratis · May 07, 2011 at 10:18 PM 0
Share

Thanks Eric. Great code as usual.

avatar image
1

Answer by Ashkan_gc · Apr 27, 2011 at 03:24 AM

the problem is that InvokeRepeating is called multiple times in multiple frames (whenever the condition is true) so multiple instances of your code will reduce the fuel. use coroutines for the stuff of change your Update method to check the condition and reduce the fuel itself or stoppo all invoked functions before invoking new ones.

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 Tommy · Apr 30, 2011 at 12:55 PM

Maybe you can do something like this:

var timeTilYouRunOutOfFuel = 5.0; var totaltTime;

function Update() {
totalTime = Time.time + timeTilYouRunOutOfFuel; if (Time.time <= totalTime) { //Keep on flaming } else if (Time.time >= totalTime) || (Time.time == totalTime) { //Crap, out of fuel, stop flaming. } }

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 MadeThisName · Jun 21, 2015 at 07:21 AM

Beautiful Eric5h5 Thank you for all your posts they have taught me a lot. Here is a C# version of Eric5h5 script.

 using UnityEngine;
 using System.Collections;
 
 public class MYCLASSNAME : MonoBehaviour {
     
     
     public float maxFuel = 100.0f; 
     public float fuelUsageRate = 10.0f; 
     public float fuelRefillRate = 100.0f;
     private float currentFuel;
     
     void  Awake ()
     { 
         currentFuel = maxFuel; 
         StartCoroutine (FireControl());
     }
     
     IEnumerator FireControl()
     {
         while (true) 
         {
             yield return StartCoroutine (WaitForInput()); 
             yield return StartCoroutine (Fire()); 
             
             if (currentFuel <= 0.0f) 
             {
                 yield return StartCoroutine (Reload()); 
             }
         }
     }
     
     IEnumerator WaitForInput ()
     {
         while (!Input.GetButton("Fire1")) { 
             yield return 0;
         }
     }
     
     IEnumerator  Fire ()
     {
         while (Input.GetButton("Fire1") && currentFuel > 0.0f)
         {
             currentFuel -= fuelUsageRate * Time.deltaTime; 
             yield return 0; 
         }
     }
     
     IEnumerator  Reload ()
     {
         while (currentFuel < maxFuel)
         { 
             currentFuel += fuelRefillRate * Time.deltaTime; 
             yield return 0;
         }
         currentFuel = maxFuel;
     } 
 }
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

1 Person is following this question.

avatar image

Related Questions

how to decrease repeating time in invokerepeating? 2 Answers

How does Invoke cope with mapping on frames 1 Answer

What is the most CPU efficient and smooth way to create timed events? 2 Answers

C# yield not doing anything 2 Answers

Decrease Health every 5 mins or Time script? 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