Hello,
I’ve used for the past years without questioning it the following interaction system, I’ve seen in a tutorial, in my first person 3D games:
- uses raycast to check the distance with an object
- the object checks using OnMouseOver() which when executed (player looking at object), it uses the distance measured before and makes the decision depending on the proximity.
However, I nowadays wonder why I simply don’t use Vector3.Distance instead of a raycast ? After all, the first raycast used is simply for the distance with the object, and I believe has more impact on the performance. Why? Simply because a dedicated script currently creates every frame a raycast to check that if an object is here, check distance. However, with Vector3.Distance, I can place it in the MouseOver and if the player has its mouse over (looking at it), check the distance and make decision.
So in conclusion, which is better and more efficient?
Thanks
1 Like
Vector3.Distance of the ‘position’ of both gameobjects is going to be the more efficient compared to a raycast. All it really is under the hood mathematically is:
len(a - b)
But that’s because you’re talking about 2 fundamentally different things.
A raycast via something like Physics.Raycast is a physics based thing. It’s more costly because well it’s a performing quite a bit of physics calculations from searching the spatial tree of the area and all that to then find a point. And then finding the distance from the start point to that end point… so effectively you’ve got the same previous task AND the physics.
So it inherently is more work.
But it also results in a different outcome. The GameObject’s position is going to be from the middle of the Transform (the point at which the position is relative). You don’t really have any more information than that.
Where as a physics based raycast is going to be to the surface of a collider. Your distance check is more spatially aware of the volume of the thing you’re measuring a distance from (replace 3d words with 2d for 2d physics).
So in the end your choice between the 2 has less to do with performance and more to do with what you need to actually measure!
2 Likes
Thanks for your reply! I think I will try the Vector3.Distance for its simplicity and see how it goes
Performance should always be the second thing you should think about. What’s more important is if the used method actually solved your problem at hand. You said “distance between two gameobjects” and depending on the situation that could mean tons of different things. Lordofduct already pointed out the techincal differences and that a raycast usually requires you to have already a position or direction to raycast against.
In many cases a simple distance check between the object’s pivots would be enough. For example if you want to know how far away you are from a door handle, in order to interact with it, a simple distance check would most likely be enough. Though for things like that you usually would use triggers in front of the door.
Though if you ant to know the distance to a house and the pivot is one of the corners you get vastly different result depending on from which side you approach. Having the pivot in the base center would be better, but it still differs quite a bit depending on if you appoach the center of a side or a corner. As I said it depends on the goal. If the house is a skyscraper and the pivot is in the geometric center half way up the building, you can really strange results when you are at street level. Yes, you could place “marker” gameobjects which you use for the distance calculation but in the end it completely depends on what you need that distance for and how accurate it should be.
You can not simply use a raycast instead since you need to know where to raycast. The center of the object? What if the object is a U shaped building. You could stand at the very center and be still outside the building. So if this was a generic question “what is better”, there is no real answer because it depends if the proposed methods actually solve the problem.
Yes a raycast is often unnecessarily more complex, requires colliders and an actual direction to raycast. So from a pure overhead and flexibility perspective, just calculating the distance between two position is certainly cheaper.
If you just need the distance to one particular object bur relative to the collider, you can also use Collider.ClosestPoint
Thanks! It would be for an interaction system, so the player can interact, grab, etc.
The distance would be needed to see if the player is close enough. The objects being quite small, I believe Vector3.Distance would work well.
In a 3d environment? How do you “choose” with what object you interact with? Having each object somehow checking the distance to the player every frame would not make much sense. In that case you usually would use a raycast to determine the object you want to interact with. Though this all depends on the type of game. 3D / 2D, first person / third person, etc.
In a 3d environment you would almost always use a raycast and limit the raycast lenght. So you naturally can only interact with object that are within that range. Still way too many unknowns here 
The original question was already quite abstract and the title is even more generic. The straight forward answer for the title is d = sqrt((x1-x2)² + (y1-y2)² + (z1 - z2)²) which is just the Pythagorean theorem or Euclidean distance which is essentially the same thing. This is exactly what Vector3.Distance does behind the scenes. However in order to interact with a specific object, you need to figure out that this object exists in the first place in order to interact with it and a raycast is a quite common approach. Triggers can be used as well in combination with distance checking. Though in many cases you usually only want to interact with something when you actually “face” it.