• 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 jjthejj · Mar 27, 2015 at 04:58 PM · collisiontimer

Starting/Stopping a Timer On/Off Collision

Hello, I am trying to create a game where you have a ball in a box (Check) And there is another ball in the box except the second ball is fixed, no physics (Check), and now I want to add a piece of code that will allow me have it so that when I jump on the fixed ball with the movable one, a timer will start, and off contact of the ball the timer will stop and it will have a value stored in a variable that I can display, from there I think I can figure out storing best time and what not, thanks (Off/On collision or something? idk how it all works)

PS I would like for the timer to be in Minutes, Seconds and Milliseconds

Ok I keep on not having my questions approved (unapproved!), I suggest you read your mod blocking rules and see that your suppose to give me a answer why it's blocked as I have no idea so will continue to submit until other wise, 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Basen · Mar 27, 2015 at 05:14 PM

Greetings,

I am still very new to this, but I would do something like this:

 float timer = 0;
 bool timerCheck = false;
 float timerMin = 0;
 float timerSec = 0;
 float timerMil = 0;
 
 void Update()
 {
 if(timerCheck)
 {
 timer += Time.deltaTime;
 }
 }
 
 void OnCollisionEnter2D(Collider2D col)
 {
 timerCheck = true;
 }
 void OnCollisionExit2D(Collider2D col)
 {
 timerCheck = false;
 }

to display the actual minutes seconds and miliseconds, I would do the following: timerMinutes = Mathf.Floor(timer/60); timerSec = Mathf.Floor(timer%60);

Not sure about milisecs... Sorry, hope this helps at least a bit...

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The timer starts after a collision with an object 1 Answer

How To Reset Timer on a Collision (C#) 0 Answers

lightup on collision 2 Answers

Add seconds to timer countdown. 3 Answers

Make a program wait for X seconds BUT Happen earlier on collision 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