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 Raccoonking · Nov 19, 2013 at 09:00 PM · timercounterfire

player on fire timer

I am trying to create a timer that begins when player OnTriggerEnter an object with the tag fire. The timer counts to 5, then resets. It is going to be used to reduce player health for a short time after player has come into contact with fire game object.

It works fine apart from when the counter resets it then won't start again if player OnTriggerEnter a second time.

I'm sure it's something very simple that I'm not yet aware of in Unity but I'm new coding. Any help/advice would be appreciated.

Thanks very much.

Here is the code:

 var playerHealth : int = 100;
 
 
 var onFire : boolean = false;
 var flameTimer : int = 0;
 
 
 function Start () {
 
 
 }
 
 
 //Tells script player has collided with fire//////////
 
 function OnTriggerEnter(other : Collider){
 
     if(other.gameObject.CompareTag("fire")){
         
         onFire = true;
                         
     }
 
 }
 
 //Converts player health to string and puts it in GUI element////////////////
 function OnGUI (){
     GUI.Label (Rect (10, 10, 100 ,20), "Health: " + playerHealth.ToString());
     GUI.Label (Rect (10, 20, 100, 20), "flameTimer: " + flameTimer.ToString());
     GUI.Label (Rect (10, 30, 100 ,20), "onFire: " + onFire.ToString());
 }
 
 
 function Update () {
 
    if (onFire == true){
     
         flameTimer = Time.time;//increments flameTimer up
         playerHealth = 100 - flameTimer;//increments playerHealth down
         
     }
     
     if (flameTimer >= 5){//if flameTimer is greater than or equal to 5 then stop player being on fire
     
         onFire = false;
     
     }
 
 
 }//UPDATE END
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 slayer29179 · Nov 19, 2013 at 09:13 PM 0
Share

If you remove if (flameTimer >= 5){

         onFire = false;
  
     }

It will not disable itself. That is the reason it won't repeat. You will also need to make: if (onFire == true){

         flameTimer = Time.time;//increments flameTimer up
         playerHealth = 100 - flameTimer;
 
 if (flameTimer < 1)
 {
        flameTimer = 5; //Reset the flame timer.
 }
  
     }

Hope this guides you!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Nov 19, 2013 at 09:54 PM

Maybe you should try using the InvokeRepeating method.

 InvokeRepeating("Fire", 0, flameTime);
 

Then, to cancel firing, call CancelInvoke.

 CancelInvoke("Fire");

Of course, you'll need a Fire method:

 function Fire()
 {
     // firing logic goes here
 }


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 Raccoonking · Nov 20, 2013 at 02:28 AM

Thank you slayer29179 and iwaldrop. Much appreciated. In the end I found a different approach which may be of interest.

I used a yield WaitForSeconds (5); http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds-ctor.html

Ended up being a tidy way to write it. It's not that precise, I end up removing 6-7 points from player health but for the time being it will do.

Thanks again for the help :)

Here it is:

 var playerHealth : int = 100;
 
 
 
 //TRAPS DAMAGE///////////////////////////////////
 var onFire : boolean = false;
 
 
 
 
 //on fire timer
 function OnTriggerEnter(other : Collider){
 
     if(other.gameObject.CompareTag("fire")){
         
         onFire = true;
         yield WaitForSeconds (5);//wait for 5 seconds
         onFire = false;
                         
     }
 
 }
 
 
 function Start () {
 
 
 }//START END///////////////////////////////////
 
 
 
 
 
 
 //Converts player health to string and puts it in GUI element////////////////
 function OnGUI (){
     GUI.Label (Rect (10, 10, 100 ,20), "Health: " + playerHealth.ToString());
     GUI.Label (Rect (10, 30, 100 ,20), "onFire: " + onFire.ToString());
 }
 
 
 
 
 
 function Update () {
 
 
 //Flame ///////////////
 
 if (onFire == true){
 
     playerHealth = 100 - Time.time;
 
 }
 
 
 
 
 }//UPDATE END 


Comment
Add comment · Show 2 · 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 iwaldrop · Nov 20, 2013 at 06:02 AM 0
Share

What you're saying is that for five seconds after entering a collider with the tag of "fire" the playerHealth will be set to 100 - the time of that frame since startup.

Time.time is the time of the frame since startup. Your logic states that if this occurs more than 100 seconds after the beginning of the game, then the player's health will be set to a negative number.

This kind of logic is flawed, because the player can enter and exit these zones at will, right? So what happens if he enters it at 5 seconds, and then again at 9 seconds? Will it still stop hurting him at 10? Yes, because the collision event is setting onFire to false. Ideally you want to call a method from the collision that handles whatever you want to do, like so:

 var fireDuration : float;
 var fireDamagePerHit : float;
 
 private var isFiring : bool;
 
 function OnTriggerEnter(other : Collider)
 {
     ProcessTriggerEntered(other.tag)
 }
 
 function ProcessTriggerEntered(string tagName)
 {
     switch(tagName)
     {
     case "fire":
         Fire();
         break;
     }
 }
 
 function Fire()
 {
     if (isFiring)
         return;
     float startTime = Time.time;
     float endTime = startTime + fireDuration;
     while (endTime > Time.time)
     {
         playerHealth -= fireDamagePerHit;
         yield return null;
     }
     isFiring = false;
 }

Sorry if some of this doesn't compile, I work in C#. :)

Also, if you want multiple things to damage the player simultaneously then you need to track what is damaging him and increment/decrement a counter when firing starts and stops.

But all of this is an aside to the fact that this is the wrong way to do it. You should attach a script that will deal damage the player, not attach a script to the player that will damage himself based on whether he has collided with something.

avatar image iwaldrop · Nov 20, 2013 at 06:58 AM 0
Share

While everything I said is technically true, I just noticed what you're trying to do. Set the player on fire. Your Boolean flag should be called isOnFire, not onFire. Self documenting code is the best kind of code.

In that case, you're basically right in your thinking, and the script should be somewhere on the player, so belay my last. However, it would be best to have a fire effect game object that has a particle emitter and a script. That script should activate and deactivate the fire effect, while damaging he player while it's active.

 var duration : float = 5;
 private var endTime;

 function Activate()
 {
     endTime = Time.time + duration;
     enabled = true;
 }

 function Deactivate()
 {
     enabled = false;
 }

 function OnEnable()
 {
     // start particle effect
 }
 
 function OnDisabled()
 {
     // stop fire effect
 }
 
 function Update()
 {
     // do damage to player
     enabled = endTime > Time.time;
 }

This way, you have a discrete class that handles the effect and does damage, can be activated repeatedly, and will always keep the player on fire for the duration you define.

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

19 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

Related Questions

Stop watch for 100 meter dash 1 Answer

Can anyone help ?? Timer and gui 0 Answers

How to create a homing script 1 Answer

C# countdown timer 9 Answers

How to realize accurate time counter (millisecond precision) 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