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 EmJay · May 14, 2013 at 06:47 PM · arraylistprecalculatesquare root

Precalculated array/list or Mathf.Sqrt for hypotenuse?

So you have to get square root from analog input in order to determine how fast should the character move forward. But which is better. Having lots of precalculated values where to take comparisons or use Mathf.Sqrt and calculate the exact result or a bit rounded up result. The values are always between 1 and -1.

I'm just scared of square root because its time taking calculation but these days computers have a lot of computing power in them for it being little to no problem in order to solve such a little task.

So, the best way or even better one?

PS: Add more tags and raise the tag limit. Hypotenuse.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hoy_smallfry · May 14, 2013 at 07:06 PM

You don't need to calculate the length of input by hand if you pack the values into a Vector2 and use the magnitude value. This still uses sqrt, though.

If you use sqrMagnitude it will avoid using sqrt by giving the length squared. Since there is no value with a greater magnitude of 1 along your axes, you'll never run into a a squared length greater than 1, since 1 * 1 = 1.

The relationship between numbers and their squares is a curve, like this:

alt text

You could probably use this to your advantage, because now you can have a value that gets faster at the edges and slower for sneaking when its closer to the deadzone.

Having the input max out at 1 is also benificial, because now you have a unit value. If you want your speed to be 20, you just have to multiply the square magnitude by it, which will scale your curve to a range of 0 to 20.

Comment
Add comment · Show 1 · 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 EmJay · May 15, 2013 at 04:56 PM 0
Share

I will try this and try to get comparisions by just calculating hypotenues with the square root.

Your initial post asked why I would need to calculate it. Simple, I need to see how fast should the character move and because the analogs trajectory is with in a circle so I need the length of the ray to see how fast should the character move. There for I need the hypotenues.

avatar image
0

Answer by robertbu · May 14, 2013 at 07:08 PM

It is highly unlikely that this will be your bottleneck. If you are unsure, just test it. Usually I answer these kinds of questions for myself by pushing things "beyond reason" and see what happens. So drop something like this into into your update:

 for (var i = 0; i < 100000; i++)
  {
   var foo = Mathf.Sqrt(Random.Range(0.1,0.9));
   }

On my desktop, 100,000 calculations dropped the the frame rate from 66 to 63, and part of that is the Random.Range() call. May be different on your target platform.

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
avatar image
0

Answer by Eric5h5 · May 14, 2013 at 07:12 PM

Mathf.Sqrt on a modern CPU is only a few times slower than standard math operations (and you'll find that add/subtract/multiply/divide are all pretty much the same speed too, so the whole "avoid division" thing goes out the window). In fact I found that the time taken by the square root itself on my CPU isn't so much slower than the overhead of calling the Sqrt function.

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.

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

A node in a childnode? 1 Answer

question about array or list of boolean 1 Answer

Changes not being saved in the Inspector Editor 1 Answer

Keep adding targets to a list 2 Answers

Difference between Static, Dynamic, and Built-In Arrays 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