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 tezuka · Sep 04, 2010 at 06:58 PM · gametimerunderwaterhealth

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); }

Comment
Add comment · Show 2
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 AVividLight · Sep 04, 2010 at 07:12 PM 0
Share

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...

avatar image AVividLight · Sep 04, 2010 at 07:13 PM 0
Share

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...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

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
1

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; }

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

No one has followed this question yet.

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


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