© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•12mo ago•
77 replies
ShowTime13

Is the check if variable is certain value creating performance hit?

So, here is my code in Unity (I run it in Update):
 if (currentPlatform == null && ballForce.velocity.magnitude >= 20) {
            addForceSpeedAir = addForceSpeedAir * 0.5f;
            print("forceStop1");
            if (ballForce.velocity.magnitude >= 25) {
                addForceSpeedAir = addForceSpeedAir * 0.2f;
                print("forceStop2");
            }
        } else { 
            addForceSpeedAir = originalAddForceSpeedAir; 
        }
 if (currentPlatform == null && ballForce.velocity.magnitude >= 20) {
            addForceSpeedAir = addForceSpeedAir * 0.5f;
            print("forceStop1");
            if (ballForce.velocity.magnitude >= 25) {
                addForceSpeedAir = addForceSpeedAir * 0.2f;
                print("forceStop2");
            }
        } else { 
            addForceSpeedAir = originalAddForceSpeedAir; 
        }



This code is like a speed limiter for a ball in a game.
Basically, it's doing this: If the ball is flying in the air and going really fast, it turns down the 'air boost' to slow it down a bit. The faster it's going in the air, the more it reduces this boost. But if the ball is on the ground or moving slowly, the 'air boost' goes back to normal.

So, my question is:
1. Is it a good way to change a value and then bounce back to the original value?
2. (MAIN) I'm running this in Update function, meaning 60 times per second. I'm worried that I'm going to set addForceSpeedAir to originalAddForceSpeedAir each time the if statement goes to else. So, it's going to set it once, and then each second 60 times. But, in my view only way to escape this is to Check with
else if addForceSpeedAir == originalAddForceSpeedAir
else if addForceSpeedAir == originalAddForceSpeedAir
, but then again, I'll be checking 60 times a second if a value is something, which also sounds pointless.
Is there any way to escape this?

Basically, I want to set it just once and be done with it. I don't wanna check it 60 times per second, or set it 60 times per second. Events?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ Check if list contains object with a specific property/variable value
C#CC# / help
4y ago
✅ Check if value is within range, throw exception if not
C#CC# / help
16mo ago