- Home /
How to get distances of GameObjects through SphereCast?
I've a list of gameobjects and I need to get a distance between gameobjects through RaycastHit. Is there a way to get unique distance for each gameobject looking to each other?
Answer by ShadowAngel · Dec 19, 2012 at 10:22 AM
just use Vector3.Distance. go with For thro each gameObject with thet list you have.
You gona do it like this:
var MyList : ArrayList; // This is your list of GameObjects.
var MyDistanceList : ArrayList; //This is empty list for storeing our distances.
function GetDistance()
{
for (var obj : GameObject in MyList)
{
MyDistanceList.Add(Vector3.Distance(transform.position, obj.transform.position)); //Add vaule to MyDistanceList on same index position as our target object in MyList.
}
}
So here you go a script thet creates list of distances of all gameObjects on your list with curnet one thet this script is attached to. Dont forget to use this function in Update or Start.
To reiterate, Raycasts (and SphereCasts) are when you don't know the objects. They ask "what's in that direction?"
Like if you want to check for falling, can raycast down to see if a car, a rock, the ground or whatever is below you. But, If you don't have cars and rocks, so you know the ground is the only thing below you. you can just look up the ground height where you are -- no raycast needed.
@ShadowAngel, Sure, probably should work properly, but I need to get Distance, Collision and Direction, thats why I've to use RaycastHit, actually Physics.SphereCast to cover all the data I need from the list of game objects.
@Owen Reynolds, but when I need to get the distance, collision, direction. I think there is only the way to use SphereCasts for grabbing those data from game objects or you can propound any other approaches?
"get Direction"? You have to already know the direction in order to do a *cast.
To find direction to a known object, can use Quaternion.LookRotation(pos2-pos1);
Your answer
Follow this Question
Related Questions
Is Raycast relatived to screen resolution? 1 Answer
SphereCastAll doesn't seem to be working 2 Answers
Help with Physics.BoxCast (or maybe ray casts in general?) 1 Answer
Raycast stop at distance 3 Answers