I am trying to get the shortest distance from an object of 4 different objects.
I have tried a few different things from the docs, but I can not figure out how to get this done.
Would a raycast or a spherecast work better?
I am trying to get the shortest distance from an object of 4 different objects.
I have tried a few different things from the docs, but I can not figure out how to get this done.
Would a raycast or a spherecast work better?
If you have references to all the objects just use Vector3.Distance.
if you dont, use spherecast.
I do have references, I used this-
distance = Vector3.Distance(transform.position, object.position);
But it just gives the distance, but doesn’t update it. The object is a prefab…so do I need to find the object(clone) to get the distance?
youll need to provide more code
I did this, and it works now, but is it the right way?
object= GameObject.Find("object");
test.text = "distance = " + distance.ToString();
distance = Vector3.Distance(transform.position, object.transform.position);
If you know the other four objects, this is a pretty straightforward algorithm.
So… that’s the question, isn’t it? How do you know which other four objects? If you already have references to them, the battle is halfway done. If you don’t, you’re going to have to help us to help you, because we have no idea how your project is set up and can’t see your scene.
Well I am using the code above and it works…but I would like to get a more accuate distance…is the code above giving me the distance to the center of the objects?
I have all the references like I stated above.
It’s the distance between the transforms which may or may not be the center of your objects. What about it isn’t accurate? How do you want it to be more accurate? If you want to take colliders into consideration then check out http://docs.unity3d.com/Documentation/ScriptReference/Bounds.SqrDistance.html or compare http://docs.unity3d.com/Documentation/ScriptReference/Collider.ClosestPointOnBounds.html
As far as accuracy, I mean to the outside of the mesh not the center of it.
The games object is to get an object closest to another object. I can not find a good example of a spherecast or how a raycast could work.
The code above works, but I am not sure if it is the right way to go about it.
Put a collider on it or set some kind of radius for the object in code and compare against that instead of the transform itself.
Unless the mesh is abnormally shaped and you have to have the faces as close as possible in which case the most accurate method would be to enumerate all the vertices in each mesh to determine which 2 were closes to each other and compare those.