- Home /
The question is answered, right answer was accepted
Making a repeating event that detects if you are in the rain?
I want to write a script (or multiple scripts) that randomly selects a time for an event called “rain”. When the event starts, a message will display on the corner of your screen warning you that the rain is coming. It will then start a timer, the duration will be randomly selected between 10 and 30 seconds. The timer will not be displayed. Once time is up, the rain comes down. Whenever you step into the rain you take 10 damage per second that you are in the rain. If you step out of the rain, the damage stops. The rain lasts for a randomized time between 1 to five minutes. When the rain stops, a message appears letting you know that it is safe to step out. The event then ends and will restart in half an hour to two hours.
I currently know how to use box colliders to apply damage per second, and have set up a health bar. I'm wondering if it's possible to still use box colliders to define which areas are in the rain and which are not, for example if you stand under a roof you are not in the rain, therefore you should not take damage.
Answer by Llama_w_2Ls · Aug 12, 2020 at 07:48 PM
You could also shoot a raycast directly upwards, and if it collides with anything, it means that there is a ceiling, else, youre out in the open.
Answer by Aviryx · Aug 12, 2020 at 08:16 PM
This is a pretty interesting idea but not sure how hard it might be to implement. The issues I see are that you really don't want to be "always" checking for a rain event. You would want to kickstart it yourself to have more control and use less resources.
This might point you in the right direction. You could still use box colliders and just check for when the player enters/exits a trigger area. Or... you could use layers. if your terrain/outside stuff is only specific layers you could raycast whatever is underneath the player, get the layer and apply damage if they are outside.
public class RainManager : Monobehaviour
{
// store a reference to the RainEvent script
public RainEvent rainEvent;
void Start()
{
// when the game first begins we need to specify a time until the "first" rain event.
// so check if the time until the next event is 0, if so, start the first countdown.
if (rainEvent.timeUntilNextRainEvent == 0)
{
rainEvent.StartRainEventCountdown();
}
}
}
public class RainEvent : Monobehaviour
{
public string warningMessage = "Rain Incoming! Get To Da Choppa!";
public string eventOverMessage = "it has stopped raining.";
public bool isRaining;
public int minimumTimeUntilNextRainEvent;
public int maximumTimeUntilNextRainEvent;
public int timeUntilNextRainEvent;
public int minimumRainDuration;
public int maximumRainDuration;
public void StartRainEventCountdown()
{
// store how long until the next rain event in minutes
timeUntilNextRainEvent = Random.Range(minimumTimeUntilNextRainEvent, maximumTimeUntilNextRainEvent)
countdownStarted = true;
StartCoroutine(Countdown());
}
private void StartRain()
{
isRaining = true;
int rainDuration = Random.Range(minimumRainDuration, maximumRainDuration)
}
private void StopRain()
{
//
}
private IEnumerator Countdown()
{
while (timeUntilNextRainEvent > 0)
{
timeUntilNextRainEvent -= 1;
yield return new WaitForSeconds(60);
}
StartRain();
}
}
Answer by withlovegiftgiver · Aug 13, 2020 at 05:58 AM
Ok, thanks to both of you for your input, I've managed to make it work as it should except for the fact that it doesn't loop, so this event can basically only happen once. Here is my code:
{
timeUntilNextRainEvent = Random.Range(minimumTimeUntilNextRainEvent, maximumTimeUntilNextRainEvent);
countdownStarted = true;
StartCoroutine(Countdown());
}
private void StartRain()
{
isRaining = true;
int RainDuration = Random.Range(minimumRainDuration, maximumRainDuration);
StartCoroutine(Duration());
}
private void StopRain()
{
isRaining = false;
}
private IEnumerator Countdown()
{
while (timeUntilNextRainEvent > 0)
{
timeUntilNextRainEvent -= 1;
yield return new WaitForSeconds(5);
}
StartRain();
}
private IEnumerator Duration()
{
while (RainDuration > 0)
{
RainDuration -= 1;
yield return new WaitForSeconds(5);
}
StopRain();
}
Is there a way to loop this every hour without changing the code too much?
Follow this Question
Related Questions
How to define the number of times of an event? 1 Answer
Help to trigger a rain event 1 Answer
Any way to key JUST keydown and JUST keyup events? 6 Answers
How to repeat damage 1 Answer