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
-1
Question by acidcore · Dec 16, 2013 at 06:42 PM · timerspherestopalgorithm

How do I stop a timer when a sphere gets to a certain point?

Basically I have set up a random grid using Primm's Algorithm. When the game is started the grid is randomly generated with a start point (highlighted green) which is always at (0,0,0) and a randomly generated end point (highlighted red). I have placed a sphere on the start point for the user to navigate the grid/maze, and I have also created a timer that begins to count as soon as the game is loaded, but I want this timer to stop whenever the sphere hits the end point.

The timer I use is one I found on a tutorial site (don't think I'm allowd to link to this site), but it doesn't come with a stop clause. Is there any way in which to do this?

Hope this has been clear enough, Thanks :)

Comment
Add comment · Show 3
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 acidcore · Dec 16, 2013 at 09:01 PM 0
Share

This is the code that I am currently using for the timer:

 private var startTime : float;
 var textTime : String;
 
 function Start() {
 
     startTime = Time.time;
     
 }
 
 function OnGUI () {
 
     var guiTime = Time.time - startTime;
     
     var $$anonymous$$utes : int = guiTime / 60; //This creates the $$anonymous$$utes
     var seconds : int = guiTime %60; //This creates the seconds
     var fraction : int = (guiTime * 100) % 100; //$$anonymous$$iliseconds, obviously.
     
     textTime = String.Format ("{0:00}:{1:00}:{2:00}", $$anonymous$$utes, seconds, fraction); //The way it is displayed on screen
     GetComponent(GUIText).text = textTime;
 
 }
avatar image guitarxe · Dec 16, 2013 at 09:17 PM 0
Share

Same example as I provided below can be applied here. Create a new bool, which will be true while you want the timer to run, and false when you want the timer to stop.

Then wrap the line

 var guiTime = Time.time - startTime;

within an if() using the new bool as the conditional. This way, the timer only increments if the bool is true.

Write an additional method to change the value of this bool to true or false and call these methods when and however you like. In your particular situation, you said you wanted it to stop when your sphere reaches the end point. Like I mentioned in the answer below, use a collider and OnTriggerEnter() to achieve this.

Try and get as much working as you can on your own. Search the Scripting Reference and the Component Reference to get more help, and also watch these tutorial videos that explain the use of colliders:

http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders

http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers

http://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter

If you have more questions, feel free to ask a new question with more specific details.

avatar image acidcore · Dec 17, 2013 at 02:52 PM 0
Share

Hi thanks for the quick reply! Unfortunately when I try and apply your code to my GUIText the timer is not displayed on the screen. I tried to incorporate some of the elements into the code I already have, but I feel really out of my depth working in such a way.

At the $$anonymous$$ute I have your code attached to the GUIText and as I already said it's not displaying the timer. Can you help me any further?

Thanks again for even replying :)

Here's the code right now: #pragma strict

 private var currentTime = 0;
 private var running : boolean;
 
 function Start()
 {
     running = true;
 }
 
 function Update()  
 {
     if (running)
     {
         currentTime += Time.deltaTime;
     }
 }
 
 function StopTimer()
 {
     running = false;
 }
 
 function StartTimer()
 {
     running = true;
 }
 
 function Reset()
 {
     running = true;
     currentTime= 0;
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by guitarxe · Dec 16, 2013 at 07:20 PM

Without seeing the code, we cannot really help with whatever script you are using. If you have gotten that code from some site that prohibits you from re-distributing it, then try asking for support on that site.

Otherwise, consider writing your own. A very simple timer can be done like this:

 public class Timer : MonoBehaviour 
 {
     private static float currentTime = 0;
     private static bool running;
 
     void Start()
     {
         running = true;
     }
 
     void Update() 
     {
         if (running)
         {
             currentTime += Time.deltaTime;
         }
     }
 
     public static void StopTimer()
     {
         running = false;
     }
 
     public static void StartTimer()
     {
         running = true;
     }
 
     public static void Reset()
     {
         running = true;
         currentTime = 0;
     }
 
 }

To detect your sphere object entering the end point, the simplest method would be to place a collider there and set it to be a trigger in the inspector. Then you write additional code to disable the timer when the sphere enters the trigger, utilizing the OnTriggerEnter() function.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timed particle system disable 0 Answers

Timer that stops at the end of the game 3 Answers

RotateAround x degrees over y time 1 Answer

Check for closed circuit of pickups 2 Answers

Race timer needs to stop on trigger. Help 1 Answer


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