- Home /
Are Raycasts more performance-friendly than Vector3.Distance?
Are Raycasts more performance-friendly than Vector3.Distance?
In what sense? They are both used for different things, so what are you trying to do?
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.
They are two really basic function and they are not going to slow your game down.
@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.
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.
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.
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.
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.
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.