• 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 ItsKeif · Jul 29, 2019 at 04:31 PM · floattime.time

Float values below 1f don't seem to matter with Time.time.

The script below is a simple bomb explosion script triggered at a certain point during my game. I want there to be two different sprites appearing before the bomb detonates.

The value countdownTime won't impact the changes in sprites until it goes above 1f. I'd like to have the changes happen with less than a second between them so this is rather irritating. Can someone explain what is happening here?

void Explode() { if (setCountdown == false) { countdownTime += Time.time; setCountdown = true; }

     if (Time.time > countdownTime)
     {
         spriteRenderer.sprite = boomb;
     }
     if (Time.time > countdownTime * 2)
     {
         spriteRenderer.sprite = boombTwo;
     }
     if (Time.time > countdownTime * 3)
     {
         if (exploded == false)
         {
             Instantiate(topRightFrag, topRight.transform.position, topRight.transform.rotation);
             Instantiate(topLeftFrag, topLeft.transform.position, topLeft.transform.rotation);
             Instantiate(bottomRightFrag, bottomRight.transform.position, bottomRight.transform.rotation);
             Instantiate(bottomLeftFrag, bottomLeft.transform.position, bottomLeft.transform.rotation);

             exploded = true;
         }

         Destroy(gameObject);
     }
 }
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 Atenrev · Jul 29, 2019 at 05:26 PM

Time.time returns the value of the time that has passed since the start of the game. I don't think this is the approach you want to take. Take care with that, because it won't be dynamic. Instead, I would use a variable that acted as a timer like

 timer += Time.deltaTime;

Also, I guess that what you want to do here

 countdownTime += Time.time

is what I said above. But, again, I don't know what you were trying to do, this variable I think that it should have a fixed value and be compared with the timer in the ifs like

 if (timer > countdownTime * 2)
 {
       spriteRenderer.sprite = boombTwo;
 }
 else if (timer > countdownTime)
 {
      spriteRenderer.sprite = boomb;
 }
 else ...
 

Hope I helped you.

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

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

Is there an int based class/struct like Vector2? 4 Answers

create graph editior for values 0 Answers

How to change the variables in my argument through a function? 2 Answers

Reference problem 1 Answer

Execute on one frame using a float - Been looking for a solution for a while now... 1 Answer

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