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 $$anonymous$$ · Oct 01, 2018 at 04:45 PM · not workinginvokerepeatingnot responding

InvokeRepeating doesn't react

https://youtu.be/3FPO72a9N3A

So, I wanted to Implement so-called ,,Coyote Time".


I planned the Script to go something like this:

  1. The Time, how long the Player was in the Air, should be recorded using the int AirTime Variable.

  2. If the Player is in the Air for the Right Amount of Time, a Second Ground Check behind the Main Ground Check should appear.


It's proven that the OnTriggerEnter2D(){} and OnTriggerExit2D(){} Parts work, thanks to the Debug.Log(); Lines, but the InvokeRepeating(); somehow doesn't work. I can't understand, why the InvokeRepeating(); doesn't react at all, because the Rest of the Code in OnTriggerEnter2D(){} (and even OnTriggerExit2D(){} ) works. Normally, the int AirTime Variable (See Video) should go up, as long as the Player is in the Air, and should only stay at 0, as long as the Player is on the Ground. Please take a look at my Script and hopefully you can tell me what's wrong. Before telling me your Solution, please keep in Mind, that I want the Calculations for the AirTime Variable to be Frame-Rate-Independent, if possible. If you've told me a Solution, that's not Frame-Rate independent, and then found a Solution, that's actually Frame-Rate Independent, please come back and Answer with the Frame-Rate Independent Solution.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AirJumpPeriod : MonoBehaviour
 {
 
     public float AirTime = 0; //How long you've been in the Air and not on the Ground. Will be resetted whenever you go to the Ground again.
     public GameObject AnotherGroundCheck;
 
     // Start is called before the first frame update
     void Start()
     {
         //InvokeRepeating("AddAirTime", 0, 0.1f); //Add Air Time every 10th of a Second
     }
 
     // Update is called once per frame
     void Update()
     {
         //If you've been in the Air for at least 0.01 Seconds, but also for less than 2 Seconds:
         if (AirTime > 0.01f && AirTime < 2f)
         {
             AnotherGroundCheck.SetActive(true); //Activate the other Ground Check
         } else //Otherwise
         {
             AnotherGroundCheck.SetActive(false); //Deactivate the other Ground Check
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         //When you hit Ground
         if (other.CompareTag("Ground"))
         {
             Debug.Log("Got on Ground");
 
             CancelInvoke("AddAirTime");
             AirTime = 0; //Reset Air Time
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         //When you leave Ground
         if (other.CompareTag("Ground"))
         {
             Debug.Log("Left Ground");
 
             //RepeatAddingAirTime(); //Add Air Time every 100th of a Second
             InvokeRepeating("AddAirTime", 0f, 0.01f); //Add Air Time every 100th of a Second
         }
     }
 
     /*void RepeatAddingAirTime()
     {
         InvokeRepeating("AddAirTime", 0f, 0.001f); //Add Air Time every 1000th of a Second
     }*/
 
     void AddAirTime()
     {
         AirTime =+ 0.001f; //Add 0.001 Air Time
     }
 }
 
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
0
Best Answer

Answer by LCStark · Oct 01, 2018 at 05:04 PM

  1. Have you tried putting a Debug.Log in the AddAirTime method to see if it fires?

  2. Check if the AirTime value changes. Perhaps something is resetting it to 0?

  3. I think that simply adding Time.deltaTime to your AirTime in Update should be enough to track air time, instead of complicated InvokedRepeating maneuvers. Or, you could use FixedUpdate and increment by Time.fixedDeltaTime.

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 $$anonymous$$ · Oct 03, 2018 at 03:03 PM 0
Share

Thank you! I've come way closer to my Destination. Now, the Counter works.


The last Thing I need before the Calculations are correct, is a Way to keep the AirTime resetting to 0 as long as I stay on the Ground. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class AirJumpPeriod : $$anonymous$$onoBehaviour
 {
 
     public float AirTime = 0; //How long you've been in the Air and not on the Ground. Will be resetted whenever you go to the Ground again.
     public GameObject AnotherGroundCheck;
 
     // Start is called before the first frame update
     void Start()
     {
         //InvokeRepeating("AddAirTime", 0, 0.1f); //Add Air Time every 10th of a Second
     }
 
     // Update is called once per frame
     void Update()
     {
         AirTime = AirTime + Time.deltaTime;
 
         //If you've been in the Air for at least 0.01 Seconds, but also for less than 2 Seconds:
         if (AirTime > 0.01f && AirTime < 2f)
         {
             AnotherGroundCheck.SetActive(true); //Activate the other Ground Check
         } else //Otherwise
         {
             AnotherGroundCheck.SetActive(false); //Deactivate the other Ground Check
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         //When you hit Ground
         if (other.CompareTag("Ground"))
         {
             Debug.Log("Got on Ground");
             AirTime = 0; //Reset Air Time
         }
     }
 }

Is there something between OnTriggerEnter2D and OnTriggerExit2D, that's called as long as both Triggers keep Collided with each other (Just like Update, except it stops if both Triggers are no Longer collided with each other)?

avatar image LCStark $$anonymous$$ · Oct 03, 2018 at 03:21 PM 0
Share

https://docs.unity3d.com/ScriptReference/Collider2D.OnTriggerStay2D.html

Couldn't you use a flag to keep track of that? Set a bool field to true when you leave the ground and to false when you hit the ground, then in Update increment the timer if the field is true and zero it if the field is false.

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

91 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Unity Hub not opening my project 0 Answers

Why is InvokeRepeating not working in boolean method? 0 Answers

,Enemy AI rapidfires and invokerepeating doesn't do anything. c# 1 Answer

Unity 5.5.2f1 crashes after creating new project or loading existing project 1 Answer

Can't click on Input fields. 3 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