- Home /
Raycast Arrays
Hello all, I am trying to do a collision detection and object avoidance script for a moving around in 3d space. I think I am going to try this with a system of 14 different Raycasts. Problem is, I am trying to figure out a way to store each Racycast within an array, that way I can easily reference each raycast though out the script. I can make the array of raycasts, but I cannot figure out how to set each element of the array to an individual ray, or is that even possible? any help would be appreciated.
That is working, but what is not working is when I try to define each element.
CastArray[0] = RaycastHit f;
I get an error that say "expected ; " after the Raycast. I am not sure if I am even doing this right. Cannot seem to find an example.
I get an error that say "expected ; " after the Raycast.
You don't need to define "f" as a Raycasthit. It should already be defined earlier as a hit.
RaycastHit[] CastArray = new RaycastHit[14];
RaycastHit f;
if (Physics.Raycast (transform.position, -Vector3.up, f)) {
CastArray[0] = f;
}
so that seems to be working but now it is casting my ray from the objects transform (this is good) to world position 0,0,0 until i hit a collider (this is not what i want), I want it to cast out infront of the object at all times for a distance of 50
RaycastHit f;
if (Physics.Raycast (transform.position,transform.forward,out f,50))
{ castArray [0] = f;
if (f.transform != transform && f.collider.tag != "Terrain");
fDetected = true;
}
what can i do to fix this issue?
Your raycast call parameters look ok, perhaps a Debug.Log() command to output their values would help figure it out. I would double check the value of tranform.forward in particular.
Answer by JMurray · Feb 03, 2015 at 05:45 PM
RaycastHit[] castArray = new RaycastHit[14];
RaycastHit[] f = Physics.RaycastAll (transform.position,transform.forward,50);
for (int i = 0; i < f.Length; i++)
{
if (f[i].transform != transform && f[i].collider.tag != "Terrain")
{
castArray [0] = f[i];
fDetected = true;
}
}
Per the comments the following code worked for you.
However, unless their is preceding code that loops through the 14 expected RayCastAll calls your castArray will be:
castArray[0] = possible hit
castArray[1] = null
castArray[2] = null
castArray[3] = null
castArray[4] = null
castArray[5] = null
castArray[6] = null
castArray[7] = null
castArray[8] = null
castArray[9] = null
castArray[10] = null
castArray[11] = null
castArray[12] = null
castArray[13] = null
I got an email update on this thread from you (that for some reason I don't see on this question) that said:
Will this cast all 14 rays in the same direction? I want each of the 14 rays to be >casted out in a different direction (forward, -forward, up, -up, right, -right, etc.)
With the code I posted it wont cast in 14 directions at once.
Even if you have preceding code that casts in 14 directions then loops through each RaycastHit[], I would recommend you use SphereCastAll.
SphereCast and SphereCastAll cast a sphere from your originating point. This sphere can travel but doesn't have to.
SphereCast can be used to detect an explosion radius to tell all objects hit by the sphere to take damage (think like a grenade).
SphereCast can also be used to draw a fat ray from the transform to detect for objects in relative position. This is helpful when you have a first person player trying to interact with an object. Using a Ray is a fine line and if the object is small you have to be very accurate and your FPS camera has to be lined up with where the raycast is being drawn from. Instead using a spherecast can make it so the user doesn't have to be exactly on the object but rather the object they are interacting with just has to be in their window since its drawing a fat array.
Mind my crude drawing but there isn't enough detailed information on SphereCasting.
I believe if you are trying to just get all objects that is NOT the transform and is NOT tagged as "Terrain" in ALL directions you should do the following.
RaycastHit[] castArray = Physics.SphereCastAll (transform.position,50,transform.forward,0);
for (int i = 0; i < castArray.Length; i++)
{
if (castArray[i].transform != transform && castArray[i].collider.tag != "Terrain")
{
fDetected = true;
}
}
Also, instead of using a tag. If you set your Terrain to Layer you can exclude all terrain from your SphereCast using its optional Layermask (this is integer based on the integer id of the layer).
If you set Terrain to integer 9 in EDIT -> Project Settings -> Tags and Layers.
Then simply use
RaycastHit[] castArray = Physics.SphereCastAll (transform.position,50,transform.forward,0,9);
if(castArray != null)
{
fDetected = true
}
You wont get 14 direction array but rather an array of all objects within 50 units of your transform. This could be 1 object, this could be 1000 objects.
I hope this does help you and I didn't go off on a tangent. Good luck!
That's awesome thanks for helping me out with this. great explanation, cannot thank you enough!
Hi J$$anonymous$$urray, just to add to this. How can i make it so that the object which has the SphereCast script on, simply has the sphere around it? Ins$$anonymous$$d of specifying a distance?