Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 superluigi · Mar 09, 2014 at 12:13 AM · timeif-statements

if(float == Time.time) not working, why?

I assume that maybe Unity is skipping over a few numbers and is missing the number that it is supposed to match. I have noticed that Time.time has like 5 numbers after the period so that could explain why. However I will ask first before trying to work around the problem. I was also think about maybe using clamp to say if(float == (any number inside the clamp)) but this probably wont work. I have other solutions that WILL work but I want to try using == Time.time first. Here's some code to help you understand

 var hpRayStall : float;
 Update(){
 if(Input.GetButtonDown("P2hp"))
         {
             hpRayStall = Time.time + 2;
         }
         if(hpRayStall == Time.time)
         {
             var hpRay = Physics.Raycast(lp.transform.position, transform.TransformDirection(Vector3.right), 2);
         }
         if(hpRay)
         {
             sceneManager.GetComponent(SceneManagerScript).p1HP -= 6;
         }
Comment
Add comment · Show 1
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 Ebil · Mar 09, 2014 at 10:22 AM 0
Share

You should genereally never == two floats/doubles, especially one like Time.time. The comparison is not 100% precise. Better check with x = y

1 Reply

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

Answer by highpockets · Mar 09, 2014 at 01:02 AM

I think you may find that a coroutine will work better. I'm thinking that maybe your problem is due to the fact that Time.time might not ever be exactly the number that you are looking for because of the small gap between each update. If you put this in a coroutine, you can guarantee the execution of the code.

Comment
Add comment · Show 7 · 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 superluigi · Mar 09, 2014 at 01:13 AM 0
Share

Yea that's what I was thinking too. I figure it's jumping over the exact number. Usually I see people saying to avoid coroutines. Honestly I'm not sure what coroutine means. I assume it's yield WaitForSeconds.

avatar image highpockets · Mar 09, 2014 at 01:32 AM 0
Share

avoid coroutines!! Who said that???

Avoid the Update function, because it processes every frame which is not very optimized if you don't need it. I don't think I have a single line of code in the update function because of that. Coroutines are great. I think, maybe the only thing to worry about when you use a coroutine is to make sure that you don't start multiple instances and you don't forget to stop it when you don't need it anymore if you have a while loop in it.

Anyways:

 bool inRoutine = false;
 
   function Update()
     {
     
     if(Input.GetButtonDown("P2hp"))
     {
 
     if( !inRoutine )
     {
     inRoutine = true;
     StartCoroutine( RayStall() );
     }
     }
     }
     
     function RayStall()
     {
     
     yield WaitForSeconds( 2 );
     //your ray code...
     inRoutine = false;
     }

I also put in a bool to ensure that you don't start multiple coroutines.

avatar image superluigi · Mar 09, 2014 at 07:08 AM 0
Share

Ok just so were in agreement, Unity is probably skipping over the exact number correct. It was the only reason I could think of and you came up with the same conclusion so I guess it's unanimous.

avatar image highpockets · Mar 09, 2014 at 10:00 AM 0
Share

It's very likely that that is the case. I wouldn't put any kind of blame on unity, Time.time is a float and update runs every frame, lets say you have 30 fps, 'on average', that means Time.time will be accessible 60 times in the update function over that 2 seconds that you are waiting for the results. There is certainly no guarantee that the 60th time will be exactly 2 seconds (it might be 2.001 or 1.9999), as frame rates are constantly fluctuating. You probably miss it by a millisecond or 2 every time. The chances you hit it right on the money are probably less than your chances at winning a scratch and win, though I've never had any luck with the lottery!!

avatar image Polymo · Mar 09, 2014 at 10:49 AM 0
Share

just an idea. You could try to check if float >= Time.time-Time.deltaTime && float

Show more comments

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

23 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

Related Questions

TimeSpan does the difference automatically count the minutes 1 Answer

if and invoke repeating in start... 1 Answer

start timing when value is reached 1 Answer

Unity freezes after running this basic code. 2 Answers

audio troubles changing from one to another (stop it consistently changing) 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