- Home /
The question is answered, right answer was accepted
For each Raycast?
I has this code here and it works but only on the one ray.
What it does is cast 3 rays out one goes left, one goes right and one right in front. Like a field of view, but only with 3 rays. I intend it to be like that now, its not supposed to be a sweep.
So yeah how to get the hits from all three rays to work with the if? I can only get the left one to work.
Vector3 forwd = headdummyBone.TransformDirection(Vector3.forward);
Vector3 left = headdummyBone.TransformDirection(new Vector3(-30, -2. 10));
Vector3 right = headdummyBone.TransformDirection(new Vector3(30, -2. 10));
RaycastHit hit;
if(Physics.Raycast(headdummyBone.position, fwd, out hit, maxSeeDistance, ignoringLayers)){
if(Physics.Raycast(headdummyBone.position, left, out hit, maxSeeDistance, ignoringLayers)) || Physics.Raycast(headdummyBone.position, right, out hit, maxSeeDistance, ignoringLayers)){
if(hit.transform.tag == "Player"){
canFollow = true;
}}}}
Actually 3 raycast worked. You just need an array to store information for each raycast (if it works) like:
RaycastHit[] hit = RaycastHit[3];
if(Physics.Raycast(...,fwd,out hit[0],...)
{
if((Physics.Raycast(...,left,out hit[1],...)||(Physics.Raycast(...,right,out hit[2],...))
}
...
and get the info from the array hit.
If you want to check every direction, don't put one statement inside of another.
if(//Raycast in 1st direction)
{
//DoSomething
}
if(//Raycast in 2nd direction)
{
//DoSomething
}
if(//Raycast in 3rd direction)
{
//DoSomething
}
Code would only execute within each if() statement if the statement is true. AFAI$$anonymous$$, this will not effect cpu, unless you have an old Pentium II or something lol. If this works, please vote up (thumbs up) and accept (check mark) the answer, happy developing :D
P.S. Having your left and right statements inside the forward statement means you'd have to be hitting something in the fwd direction in order to use the left or right directions, meaning you would only be able to hit left or right casts ONLY if something was hit in fwd direction.
If this is used within Update(), this is running every frame, just to clarify, if you are running 60 frames per seconds, you're checking for hits 60 times per second with one RaycastHit ins$$anonymous$$d of 3. Checking for one thing is more efficient than checking for 3, especially 60 times per second, even 30 times per second. And you are correct, the difference won't be noticed, at least on most computers these days.
Answer by verenion · Aug 12, 2013 at 02:57 PM
This code is saying:
If the FRONT ray hits something AND the left OR right hits something, do this... etc.
That's not right is it? canFollow = true; will only run when something intersects the front AND one of the two sides.
You also are using the same raycast hit, you need to use an array of RaycastHits, as suggested by someone else, or simply define three raycasthits for each direction..
Try something like this (NOTE, this is untested, this is simply to get you on the lines of what you need to do.)
RaycastHit hitLeft;
RaycastHit hitFwd;
RaycastHit hitRight;
// you are seperating each hit by an OR - so IF (front hits) OR (left) OR (right)
// also note, each raycast, needs a different hit to use.
if(Physics.Raycast(headdummyBone.position, -headdummyBone.right, out hitLeft, maxSeeDistance, ignoringLayers) || Physics.Raycast(headdummyBone.position, headdummyBone.right, out hitRight, maxSeeDistance, ignoringLayers) || Physics.Raycast(headdummyBone.position, headdummyBone.foward, out hitFwd, maxSeeDistance, ignoringLayers)) {
// one of the rays hit something. Check each hit for tag too.
if(hitFwd.transform.tag == "Player" || hitRight.transform.tag == "Player" || hitLeft.transform.tag == "Player"){
// is the object it hit a player?
canFollow = true;
}
}
Err... it doesn't work. Only seems to detect the first Physics.Raycast.
In this case left.
The rays cast fine on the debug though. Can't be vectors... could it?
try and seperate your 3 raycasts, and display the bool of each raycast with a debut, and (if it returns true) the hit collider.
But to me, your 3 raycasts should be working.
Also, rather than using the vectors, you can just use transform.forward and transform.right this does exactly the same as your vectors, with less work.
Alright cool. Thanks dudes.
Thanks. $$anonymous$$y AI is working! I use a collider also to do the fine detail checking ins$$anonymous$$d of a million rays(which causes LAG).
So thanks again.