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 /
  • Help Room /
avatar image
0
Question by isteffy · Sep 06, 2015 at 01:28 PM · collisionbuildtimertime.timecooldown

How make: Timer with Time.time

I have a builder who summons a building 10 seconds after colliding with a 2DBoxCollider. I've taken examples from multiple cooldown answers on the forums and all use something similar to what I will show you. Leaving the 2dboxcollider will cancel this build time.

The Problem is that the building is summoned instantly whether or not i set the cooldown to 10f or 1000f.

C#

 private float summoningStart = 0f;
     public float summonCooldown = 10f;
     private bool collisionMade = false;
     void OnTriggerEnter2D(Collider2D col) {
         if (col.gameObject.tag == "Red Warrior Mage") {
             collisionMade = true;
         }
     }
 void SpawnGenerator() {
     Instantiate(generator, transform.position, Quaternion.identity);
     collisionMade = false;
     summoningStart = Time.time;
 }
 void Update() {
     if( Time.time >=  summoningStart + summonCooldown &&  collisionMade == true){
         SpawnGenerator();
     }    
 }

And to cancel the build time i do this

 void OnTriggerLeave2D(Collider2D col) {
     if (col.gameObject.tag == "Red Warrior Mage") {
         collisionMade = false;
     }
 }

So again, what's happening is the building is being summoned instantly, instead of being summoned 10 seconds after OnTriggerEnter2D is called.

Extra Credit: Each second passed is recorded and can be displayed in a visual timer with numbers

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 isteffy · Sep 07, 2015 at 06:58 PM 0
Share

@Positive7, This hit the nail on the head. Please make this an answer so I can mark this correct :) :D ^_^

avatar image Positive7 · Sep 07, 2015 at 07:03 PM 0
Share

I'm glad it worked :D

1 Reply

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

Answer by Positive7 · Sep 07, 2015 at 03:54 PM

Sent from mobile so not tested but try this :

     private  float summoningStart = 0f;
     public float summonCooldown = 10f;
     private  bool collisionMade = false;
 
     void OnTriggerEnter2D(Collider2D col) {
         if (col.gameObject.tag == "Red Warrior Mage") {
             summoningStart = Time.time + summonCooldown;
             collisionMade = true;
         }
     }
 
     void SpawnGenerator() {
         Instantiate(generator, transform.position, Quaternion.identity);
         collisionMade = false;
         summoningStart = Time.time + summonCooldown;
     }
 
     void Update() {
         if( summoningStart <=  Time.time &&  collisionMade == true){
             SpawnGenerator();
         }    
     }
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

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

Timer doesnt stop on collision,How can i stop that timer on a collision 0 Answers

How to have a speed Boost in my game? 2 Answers

Invoke() does not delay my game timer 0 Answers

Start a countdown timer after a collision 1 Answer

Cooldown time on button click 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