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 BDX777 · Jan 01, 2015 at 05:59 PM · raycastvector3.distance

Are Raycasts more performance-friendly than Vector3.Distance?

Are Raycasts more performance-friendly than Vector3.Distance?

Comment
Add comment · Show 4
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 Namey5 · Jan 01, 2015 at 09:27 PM 1
Share

In what sense? They are both used for different things, so what are you trying to do?

avatar image Glurth · Jan 01, 2015 at 09:37 PM 0
Share

Depending on what you want- Vector3.sqr$$anonymous$$agnitude is faster than Distance (one square root operation faster, specifically). This value is generally useful when comparing with other vectors, rather than computing specific values.

avatar image BMayne · Jan 02, 2015 at 01:32 AM 0
Share

They are two really basic function and they are not going to slow your game down.

avatar image BDX777 · Jan 06, 2015 at 11:55 PM 0
Share

@Namey5, I'm program$$anonymous$$g a fire system and I'm trying to create a performance-friendly oil system, I'm thinking I can put the oil decal objects into a list and have a loop that checks the distance of each one to a fire object and if they are close enough they will ignite, right now I'm using several raycasts which works but I want to ensure that it runs GREAT so I wanted to know if it would make any difference. Thanks everyone for answering.

Btw by performance I meant framerate impact, I should have made that more clear.

2 Replies

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

Answer by Eric5h5 · Jan 01, 2015 at 11:01 PM

Highly unlikely, since Vector3.Distance is just a simple square root operation (which is very fast on modern hardware), whereas a raycast will need to involve the physics engine. But any "which is faster" questions should really be answered by yourself. If you don't have access to the profiler in Unity Pro, just run comparative code and time it (using the Stopwatch class or other methods). That way you get an accurate answer which isn't subject to becoming outdated.

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 ASGS_DumbFox · Jul 06, 2021 at 05:12 PM 0
Share

Sort of an old post, but for anyone curious:


My team did some tests based on our use-case - Is it more performant to use Vector3.Distance to 1000 objects and calculate their distance or should we calculate using a spherecastall with those elements on a physics layer with a detection radius.


When using Vector3.Distance to 100 elements vs 10 elements within the radius in the spherecast the results were pretty close. However, when we started pushing that number to 1000 distance-calculated objects vs. 100 spherecast elements we started seeing that the numbers would gradually get more performant (by fractions of milliseconds). When we tried this with 5,000 and 10,000 objects, it becomes more apparent when we're only calculating a portion of those objects within the spherecast radius instead of calculating the distance for all 5/10,000.


Essentially, for our use case where we have a lot of game objects, but only want to see which ones are within a certain distance, it would be more performant to raycast out and hit whatever is within range than to calculate every object's distance and see if it is within range.


TLDR: If you are checking distance for a single or small group of game objects, or if you need to check their distance regardless of range, it is better to use Vector3.Distance. However, if you need to detect a large group of objects and care about whether those objects are within range for an event to occur, it is better to use spherecast as you can limit the amount of calculations that need to run rather than running calculations for every object.

avatar image
0

Answer by BlackWingsCorp · Jan 02, 2015 at 12:24 AM

I have used both to calculate, distances, as @Eric5h5 said Vector3.Distance is definitely more performance friendly than using raycast. From my experience it the only real difference I have found is that with raycast it takes about 0.2 to 0.25 more seconds to launch, didn't affect the gameplay at all.

Comment
Add comment · Show 2 · 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 Eric5h5 · Jan 02, 2015 at 12:35 AM 0
Share

A raycast would have no effect on the time it takes to launch anything. A raycast, like nearly all functions, is completed immediately when you call it. "Slower" compared to Vector3.Distance is relative; in this case you would be talking about something like .0000025 seconds probably, certainly not 0.25 seconds. Huge timescales like that are the result of logic problems in code, not the time it takes to execute a function.

avatar image BlackWingsCorp · Jan 02, 2015 at 01:05 AM 0
Share

I forgot to tell the script what to do in case the gameobjects it's trying to access is destroyed or not set active so I guess that's probably it, other than that as I said the game works very smoothly, no input delay, no lag.

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

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

Related Questions

Raycast won't hit Collider 1 Answer

Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer

Checking boolean state using raycast 1 Answer

Raycast in Update Incorrect Results When Moving Object 2 Answers

Raycasting in a 2D side scrolling shooter 2 Answers


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