Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mattt4001 · Jul 15, 2011 at 02:56 PM · timercountdownboundary

Countdown when out of boundry

I am trying to created a countdown to end the game if someone stays out of bounds. Right now the countdown is in the update function. I don't think this is correct. At the moment it doesn't work at all, what am I doing wrong?

var mastercam : Transform; var maxX = 0; var minX = 0; var maxZ = 0; var minZ = 0; var maxY = 0; var minY = 0; var outOfBounds : boolean = false; var BufferTime = 500; var timeleft : int; var onetime = false;

 function Update () {
     if (mastercam.position.x > maxX || mastercam.position.x < minX || mastercam.position.z > maxZ || mastercam.position.z < minZ || mastercam.position.y > maxY || mastercam.position.y < minY)
     {
         if (onetime == false)
         {
             EndTime();
             var endTime = Time.time + BufferTime;
             onetime = true;
             print("onetime");
         }
         if (onetime == true)
         {
             timeleft = endTime - Time.time;
         }
         if (timeleft <= 0)
             {
             GameOver.OutOfBounds = true;
             }
         print("out of bounds");
         outOfBounds = true;
         }
     else
         outOfBounds = false;
     }

Thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Evil-Dog · Jul 15, 2011 at 03:08 PM

Not sure what's the whole "onetime" thing about and I assume BufferTime is your time limit to be outside the bounds, correct me if I'm wrong but can't you just do this

Change your timeLeft variable to float

 var timeleft : float;
 
 function Update () 
 {
     if (mastercam.position.x > maxX || mastercam.position.x < minX || mastercam.position.z > maxZ || mastercam.position.z < minZ || mastercam.position.y > maxY || mastercam.position.y < minY)
     {
         if (timeLeft < BufferTime)
         {
             timeLeft += Time.deltaTime;
             if(timeLeft >= BufferTime)
             {
                 GameOver.OutOfBounds = true;
             }
         }
         print("out of bounds");
         outOfBounds = true;
     }
     else
     {
         outOfBounds = false;
     }
 }

Beyond that if you wanna display a timer, you could either check the timeleft and display it everytime its Ceiled or Floored value changes meaning it's a new second outside the bounds.

Another method could be to trigger a yield coroutine that works every second and you interrupt that coroutine when the player is not out of bounds.

See coroutines doc Here

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 mattt4001 · Jul 15, 2011 at 03:48 PM 0
Share

Thanks! That works perfectly!

avatar image Evil-Dog · Jul 15, 2011 at 03:50 PM 0
Share

Great good to hear! Good luck

avatar image
0

Answer by Bunny83 · Jul 15, 2011 at 04:02 PM

You declared the variable endTime inside the if block so you can't use it outside. It should be declared outside of all blocks.

The onetime thing is a way how you can do it but i would prefer this way ;)

 var mastercam : Transform;
 var max : Vector3;
 var min : Vector3;
 var BufferTime = 500.0;
 
 var outOfBounds = false;
 var endTime : float; // The time normally a float
 var timeleft : int; // here's ok if you want to view only the seconds
 
 
 function Update () {
     outOfBounds = mastercam.position.x > max.x || mastercam.position.x < min.x || mastercam.position.z > max.z || mastercam.position.z < min.z || mastercam.position.y > max.y || mastercam.position.y < min.y;
     if (!outOfBounds)
     {
         endTime = Time.time + BufferTime;
     }
     timeleft = endTime - Time.time;
     if (outOfBounds && timeleft <= 0)
     {
         GameOver.OutOfBounds = true;
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Timer counts down unwanted 2 Answers

How to stop a timer with GUI button? 1 Answer

Countdown timer in minutes:seconds:milliseconds 2 Answers

Timer not work :( 0 Answers

how to stop countdown timer after move panel ? how to fix this ?,can you tell me how to stop countdown timer after move panel ? how to fix this ? 0 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