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 ronronmx · May 20, 2013 at 05:46 AM · updatemathfclamp

Performance of Mathf.Clamp vs if-else statement?

Quick question: I'm performing a rather quick operation in Update() to keep a number from going to far in both directions. I was wondering if using Mathf.Camp() is faster then using a if() else() statement?

Here's my code below:

 // The Clamp way:
 boostCharge = Mathf.Clamp(boostCharge, 0, BoostChargeFull);

 // The if() else() way:
 if(boostCharge < 0) boostCharge = 0;
 else if(boostCharge > BoostChargeFull) boostCharge = BoostChargeFull;



As you can see, nothing special...so does it even matter for this specific case?

What do you guys think?

Thanks!

Stephane

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
5
Best Answer

Answer by Julien-Lynge · May 20, 2013 at 06:55 AM

Hey Stephane, I suggest you download ILSpy or a similar program so you can look through Unity's code yourself. Here's the Mathf.Clamp function:

 public static float Clamp(float value, float min, float max)
     {
         if (value < min)
         {
             value = min;
         }
         else
         {
             if (value > max)
             {
                 value = max;
             }
         }
         return value;
     }
Comment
Add comment · Show 3 · 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 ronronmx · May 20, 2013 at 07:43 AM 0
Share

Hi Julien, thanks for the tip, I'll look into it for sure! So basically the Clamp function does exactly what I wanted to do with my if() else() block...saves me the trouble hahaha :)

Thanks again!

avatar image MountDoomTeam · May 20, 2013 at 01:04 PM 0
Share

ILspy sounds awesome, do you mean it would be possible to see the source of something like -recalculate normals- ? it would be great for multithreading reasons.

avatar image Julien-Lynge · May 20, 2013 at 03:03 PM 0
Share

ILSpy allows you to look through dlls, for instance UnityEngine.dll. However, you'll definitely find that there are limitations, and RecalculateNormals is one of those. $$anonymous$$any methods are implemented in internal libraries outside the dll, and sadly those are much harder to peruse. Here's a bit more info:

http://answers.unity3d.com/questions/440089/where-does-extern-point-in-unityenginedll-methods.html

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

15 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

Related Questions

Implementing mathf ? 1 Answer

Modify distance with var? 1 Answer

limit accelerometer controlled rotation 1 Answer

How do I change a spot light to a point light by pressing a key? 1 Answer

How do I find the inverse cosine ??? 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