• 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 jay513 · Oct 08, 2019 at 05:23 AM · time.deltatimewhile-loop

Time.deltaTime not working with while loop

So I'm making a little fighting game for the first time and I need to check if something is overlapping with my character's hurtbox. So I made this while loop with Time.deltaTime to keep checking if it's registered a hit within the time frame given. However, as soon as it's called it just counts the timer down from 100% to 0% instantly.

 private void LaunchAttack(float damage,string animatorString, Collider2D collider)
 {
     bool breaker = true;
     float curAttackTiming = attackTiming;
     canAttack = false;
     curDamage = damage;
     myAnimator.SetBool(animatorString, true);
     while (curAttackTiming > 0)
     {
         Debug.Log(curAttackTiming);
         curAttackTiming -= Time.deltaTime;
         var enemyCol = Physics2D.OverlapBox(collider.bounds.center, collider.bounds.extents * 2, 0, LayerMask.GetMask("EnemyHitbox"));
         if (enemyCol & breaker)
         {
             Debug.Log("LaunchAttack enemyCol & breaker");
             enemyCol.GetComponentInParent<Health>().SubtractHealth(curDamage);
             breaker = false;
             curDamage = 0;
         }
     }
 }

As soon as I start the attack it gives me all this instantly: https://cdn.discordapp.com/attachments/496103505317330944/630928579655368704/unknown.png

How would I be able to make a proper timer? I just need to be able to keep checking for overlaps only when specified. I hope that made sense and thanks in advance.

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 revolute · Oct 08, 2019 at 02:29 AM

Unity updates Time.deltaTime before it enters a phase. Essentially, each "Update", "FixedUpdate" and others are grouped into collective groups and before Unity internally runs each of the groups, Time.deltaTime is updated. This means that during the same phase, deltaTime remains same. Calling Time.deltaTime in the same frame in the same group will result in same value. You need to "let go" of current loop and wait for next update.

The "let go" practically means you need to yield, and in this case I recommend using coroutines.

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 jay513 · Oct 08, 2019 at 04:22 AM 0
Share

But then I wouldn't be able to keep checking if something is overlapping the hurtbox right? Wouldn't the coroutine just check it once before or after the yield? I need to be able to check if something is overlapping during the animation in case something enters the hurtbox slightly after the attack is initiated that way the player doesn't feel cheated in niche circumstances.

avatar image revolute jay513 · Oct 08, 2019 at 06:25 AM 1
Share

Yes you can check it. while(true) yield return null; would allow you do check every frame as long as the coroutine is alive.

Depending on which function and how animation updates are handled, you either know the overlap happens on the exact frame or in the next frame. That behavior is hard to define since I have no knowledge which update you are using for animations.

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

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

Decrease a value every second? 3 Answers

What is wrong in my code - Unity freezes 1 Answer

start coroutine everytime 2 Answers

Moving out of collision with trigger using while loop crashes Unity. 1 Answer

Can you not use while loops in unity? if so how can i change this to work,Can you not use while loops in unity? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges