- Home /
unity underwater game
hello good day! i'm new to unity, i created a timer for my game, and my game includes being underwater. I put the timer inside a GUI text. I want to decrease the HP for every 5seconds underwater. here's the code.. tnx for your help!
private var passedTime ; private var minutes : int ; static var seconds : int ; private var startTime = timeVar; private var startTime2 = timeVar; private var startTime3 = timeVar; private var quitGameOverP : boolean =true; var secondo ;
function Update() { if(id==true){ var theTime : String ; var timer ; if (GUIPrompt.hard== true&& GUIPrompt.medium == false && GUIPrompt.easy == false)
{ timer = startTime ; }
else if (GUIPrompt.medium== true&& GUIPrompt.hard ==false && GUIPrompt.easy == false){ timer = startTime2 ; }
else if (GUIPrompt.easy== true){ timer = startTime3 ; }
passedTime = timer - Time.time ;
minutes = Mathf.Floor(passedTime / 60) ; seconds = Mathf.Floor(passedTime % 60) ;
guiText.text = String.Format ("{0:00}:{1:00}", minutes, seconds); }
You should put your code into a code box... click the 1's & 0's button at the top and past your code into it...
Sorry for the double comment, but I forgot to add that you should clearly state what it is that you want... 'Cause I'm not sure... And if I'm not sure, I can't help you...
Answer by Anxo · Sep 05, 2010 at 05:19 PM
Thats a lot of code, why not just say
private var Health = 100; private var UnderWaterTime : float = 0; private var UnderWater : boolean = false;
if (UnderWater){
UnderWaterTime +=1 * Time.deltaTime; if ( UnderWaterTime >= 5){ Health -= 10; UnderWaterTime = 0; return; }
} not tested but something along that line should work.
Answer by Statement · Jan 06, 2011 at 08:35 PM
I think the following code could become a simple skeleton. I added a lot of comments, some are placeholders and some are intended usage. Basically the script enters a coroutine loop that wait until player is below water, then start a drowning sequence which is escaped by surfacing, to get air. I haven't tested this code, but I checked it for any compile errors.
I make use of InvokeRepeating to start the drowning method callback after 20 seconds, and OnDrowning should be called every 5 seconds after the initial 20 seconds have passed.
function Start() { while(true) { yield WaitDive(); OnDive(); yield WaitSurface(); OnSurface(); } }
// Called once every 5 seconds, // started 20 seconds after dive. function OnDrowning() { // Subtract health // Add audio feedback (swallow water) // Add visual feedback (flashing screen) // If death, remember to call CancelInvoke. }
// Called when we are going into water. function OnDive() { // Add audio feedback (bubbles) // Add visual blur feedback // Start drowning after 20 seconds
InvokeRepeating("OnDrowning", 20, 5); }
// Called when we are coming out of water. function OnSurface() { // Add audio feedback (gasp for air) // Remove visual blur feedback // No longer in a drowning state, cancel. CancelInvoke("OnDrowning"); }
// Obviously you need to fill in this gap. function IsUnderwater() : boolean { return false; }
// Helper that wait until is under water. function WaitDive() { while (!IsUnderwater()) yield; }
// Helper that wait until is over water. function WaitSurface() { while (IsUnderwater()) yield; }
Your answer
![](https://koobas.hobune.stream/wayback/20220613072711im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random Time question 2 Answers
drop health every fifth second 3 Answers
Health Bar to Render On Top 2 Answers
getting udp package info inside unity (GlovePIE) 0 Answers
Update not being called every frame 2 Answers