# Shoot off multiple raycasts from 1 object?

So I have a gameobject that I want to shoot off multiple raycasts, lets say 50 raycasts in 50 random directions.

Then IF it hits a collider with a set tag it applies force.

how couls I go about doing this?

Thanks

I would probably use Random.insideUnitSphere inside of a loop to create and cast 50 different rays. The collider part is trivial if you’ve read the docs.

You can use the center point of your object and rotate around it by a certain degree.

``````float angle = 360 / frequency;
Quaternion quat = Quaternion.AngleAxis(angle,Vector3.forward);
Vector3 vec = transform.forward;
for (int i = 0; i <=360 ; i = i + frequency ){
// Regular raycast using vec and transform.position
vec = quat * vec;
}
``````

The frequency will determine how many raycast you get, now many raycast may hit so you should store them in a list and find the sortest hit.distance to figure out which one is the closest to the other object.

Or you can rigidbody.SweepTest which I think does something similar.

If you need random directions then you need to do the above with the calculation of the quaternion on each frame after getting the new random value.