Trying to set up a timed sickness system

So I am trying to set up a system in which my character will become sick when out in the rain too long.I originally used a float and Time.time, then i realised that wont work because after a certain time it will always be active.

so i decided to increment the float simply using ++ and set the system up that way. However this is still causing issues, Is there a way for to to basically say if sickness has been active for period of time, then do next thing?

This is what I am currently working with, but failing. Any help would be appreciated

    void SicknessSystem()
    {

        if (Wetness >= 98f)
        {
         
            pnemoniaTime++;
        }

        if (Wetness >= 99f && pnemoniaTime > 200f)
        {
            pneumonia1 = true;
        
            pnemoniaTime++;

        }

        if (pneumonia1 == true && pnemoniaTime > 600f)
        {
            pneumonia2 = true;
            pneumonia1 = false;
            pnemoniaTime++;

        }

        if (Outside == false && NearFire == true &&  pnemoniaTime > 500f)
        {
            pneumonia1 = false;
            pneumonia2 = false;
            pnemoniaTime =0f;
            

            Debug.Log("Reset");

this is the type of structure I would start with.
Your code just has if statement after if statement. You need some else statements to help with the logic.

void SicknessSystem()
    {
        if(Outside)
        {
            //player is outside
            GetsSicker();
        }else
        {
            //player is not outside
            //and NearFire
            if(NearFire)
            {
                GetsBetter();
            )
        }
    }

void GetsSicker()
    {
        //check wetness
    }

void GetsBetter()
    {

    }
1 Like

I will try this out. I would still like to use a time system, So I can if he is outside and sick for a specific amount of time, he will get sicker. I dont want to have it so that it happens instantly. Do you know a way to do this

You could use a coroutine:

IEnumerator WaitAndCheck(float delay){
yield return new WaitForSeconds(delay);
//check stuff
}

My script was just an outline. not full code.

You’ll need to tell me about the scene. I assume you have a script on the player. This script detects when the player is outside, some kind of collider or something. doesn’t really mater. But when it’s outside you have to start the timer. I assume the bool Outside is set to true.
I like the timer idea of. typically I do a Time.Deltatime
float time = 0;
time += Time.Deltatime;
if(time > 98f)
{
//do something
}

is Wetness your outside time float?

This isn’t great, but maybe it’s a start for you. The issue is that I’m not using the outsidetime to do anything.
and the wetness can go to negative numbers.
Sorry for any typos, I free handed the code.

float OutSideTime = 0;

void SicknessSystem()
    {
        if(Outside)
        {
            //player is outside
            OutSideTime += Time.DeltaTime;
           
            if(Raining)
            {
                //it's raining so wetness will increase
                Wetness++;
                GetsSicker();
            }else
            {
                //not raining, do nothing
            }
        }else
        {
            //player is inside
            OutSideTime = 0;

            if(NearFire)
            {
                //at a worm fire
                Wetness = Wetness - 5; // dry fast
                GetsBetter();
            }else
            {
                // not at a fire
                Wetness--; // dry slow
                GetsBetter();
            }
        }
    }

void GetsSicker()
    {
        //check wetness
        if (Wetness >= 99f && pnemoniaTime > 200f) // always start with the least likely this to be true
        {
            pneumonia1 = true;
            pnemoniaTime++;
        }else if (Wetness >= 98f)
        {
        
            pnemoniaTime++;
        }
    }

void GetsBetter()
    {
        pnemoniaTime--;
    }

updated the script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SicknessSystem : MonoBehaviour {
    [SerializeField]
    float OutSideTimeInRain = 0;
    [SerializeField]
    bool Outside;
    [SerializeField]
    bool Raining;
    [SerializeField]
    bool NearFire;
    [SerializeField]
    float Wetness;
    [SerializeField]
    float pnemoniaTime;
    [SerializeField]
    bool pneumoniaStage1;
    [SerializeField]
    bool pneumoniaStage2;
    [SerializeField]
    float maxWetNess;
    [SerializeField]
    float pnemoniaStage1Time;
    [SerializeField]
    float pnemoniaStage2Time;

    // Use this for initialization
    void Start () {
        Wetness = 0;
    }
   
    //main loop
    void Update()
    {
        if (Outside)
        {
            //player is outside

            if (Raining)
            {
                OutSideTimeInRain += Time.deltaTime;
                if (OutSideTimeInRain > 10f) //this give the play 10 sec tobe outside in the rain befor getting sick
                {
                    //been outside in the rain for a long time
                    GetsSicker();
                }
            }
            else
            {
                //not raining, do nothing
            }
        }
        else
        {
            //player is inside
            OutSideTimeInRain = 0;

            if (NearFire)
            {
                //at a worm fire
                if(Wetness > 0)
                {
                    Wetness--; // dry off
                }
                else
                {
                    Wetness = 0;
                }
               
                GetsBetter();
            }
            else
            {
                // not at a fire
                GetsBetter();
            }
        }
    }


    //---
    // Add / Subtract illness
    //---

    void GetsSicker()
    {
        if (Wetness < maxWetNess)
        {
            Wetness++;
        }
        //check wetness
        if (Wetness >= 990f && pnemoniaTime > pnemoniaStage2Time) // always start with the least likely this to be true
        {
            Debug.Log("Player has pneumoniaStages");
            pneumoniaStage2 = true;
        }
        else if (Wetness >= 990f && pnemoniaTime > pnemoniaStage1Time) // 60 seconds befor getting pnemoniaStage1
        {
            Debug.Log("Player has pneumoniaStage1");
            pneumoniaStage1 = true;
            pnemoniaTime++;
        }
        else if (Wetness >= 600f) // 600 whatevers befor getting pnemoniaStage1
        {
            pnemoniaTime++;
        }
    }

    void GetsBetter()
    {
        if(Wetness == 0f && pnemoniaTime > 0)
        {
            pnemoniaTime--;
        }

        if(pnemoniaTime < pnemoniaStage1Time)
        {
            pneumoniaStage1 = false;
        }
        else if (pnemoniaTime < pnemoniaStage2Time)
        {
            pneumoniaStage2 = false;
        }
    }


    //---
    // Trigger stuff
    //---

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Trigger enter" + other.name);
        if (other.name == "Building")
        {
            Outside = false;
        }
        if (other.name == "Fire")
        {
            NearFire = true;
        }
        if (other.name == "Rain")
        {
            Raining = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        Debug.Log("Trigger Exit" + other.name);
        if (other.name == "Building")
        {
            Outside = true;
        }
        if (other.name == "Fire")
        {
            NearFire = false;
        }
        if (other.name == "Rain")
        {
            Raining = false;
        }
    }
}

I have not tried out the code yet, but just wanted to update with the questions you asked.
The wetness increases when it is raining and the player goes outside. The pnemoniaTime was what I was using as a float to keep track of time.

The way I had it somewhat works, but when the player goes inside and next to fire, the pnemoniaTime continues to count, until around 200. Im not sure why it continues to count for that short amount of time, but if it didnt it would work.

I will try out the code and let you know if it works, thanks alot

wow thanks a lot. I’ve set it up and believe its working correctly.

So again, thanks a lot :slight_smile: