Is there any differences between these code?

Lecture ‘33. Improved Targeting Selection’ I went ahead and did the closest target finding a little bit different using Vector3.Distance() instead, and noticed soon after I finished the whole lecture that Nathan use different method for calculating the distance. So I’m curious that will there be any differences between these code or anything important that I should be aware of?


Yes, there is a huge difference performance wise, Vector3.Distance uses a square root operation, sqrMagnitude doesn’t, the square root operation is really, really slow, it’s better to avoid it, even more so if you are calling the function continuously.

Hope this helps!

2 Likes

Thank you for the reply, it does helps! This morning, I did some more research about the performance and found out that on mobile Vector3.sqrMagnitude can be x6 faster than Vector3.Distance, just wow! Here is the link Vector*.Distance Performance - Unity Answers

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms