- Home /
Raycast & AI problem.
I have this here AI and it is retarded. The problem is that I can't get it to stop seeing the target it looks at when going out of view like going behind another gameobject with collider.
I had actually gotten it to work(in a very bad way) before but it was all in LateUpdate which was making it lag and just kind go crazy. It just looked wrong in the LateUpdate too.
So after I gave the raycasting its own void the thing stopped detecting the colliders. The other codes I have seen don't use late update to do it so I know its something else.
So let me clarify. The rays and raycasting is working but its not detecting other colliders and or is ignoring them utterly. The AI does however detect the player with the tag and acts accordingly. It also does not detect the maximum distance of the rays which if the target exceeds then it is supposed to stop doing some things, which it does not.
Anyway here is the code or at least the part where the problem is coming from. Main part being the RayCasting().
void Update (){
AIRayCasting();
if(canFollow == true){
Following();
if(stopFollow == true){
StopFollowing();
}
}
}
void AIRayCasting(){
numRays = fovAngle;
currentAngle = fovAngle / -2;
hits.Clear();
for (int i = 0; i < numRays; i++)
{
direction = Quaternion.AngleAxis(currentAngle, transform.up) * headdummyBone.forward;
RaycastHit aimhit = new RaycastHit();
if(Physics.Raycast(headdummyBone.position, direction, out aimhit, maxSeeDistance)){
hits.Add(aimhit);
currentAngle += 1f;
Debug.DrawRay(headdummyBone.position, direction * maxSeeDistance, Color.blue);
if(aimhit.transform.tag == "Player"){
canFollow = true;
looking = true;
target = aimhit.transform;
}else{
StopFollowing();
}
}
}
}
Hope I was clear.
you do know you are contradicting yourself when you say "The rays and raycasting is working" then every thing that is said after just contradicts that.
a few things on inspection:
why is StopFollowing() being called in both Update(), and the AIRayCasting(). pick one.
where you are casting the ray from headdummyBone are you sure this bone is not set high enough to see over objects?
are you sure that maxSeeDistance is being constrained correctly?
StopFollowing() in AIRayCasting() is there just to test if it stops following. It will be something else in final.
headdummyBone is high enough I can see its detecting the player when I move into sight. Also the debug rays show me too.
The only problem is that it doesn't detect when player is behind something or out of range, just keeps going at the player.
where does canFollow get set to false?
what is looking used for (where is it set to false)?
your else feels like a fall through more then a else, and once you find the player you should stop the for loop.
there should be a break after finding the player.
Yo yeah the problem is co$$anonymous$$g at the else. I don't know how to make it stop for the loop. Can you show me?
Answer by robertbu · Aug 06, 2013 at 02:32 PM
It is hard to tell what is going on with this thin slice of your code. 'canFollow' is never set to false here. We don't see how 'stopFollow' get set, and it looks like you are calling StopFollowing() in one too many places. Plus you don't bail out of your 'for' loop if the player is spotted, which means that StopFollowing() gets called every time the raycast fails even if some of the raycasts succeed. This is likely the cause of your problem. Try putting a 'break' statement on line 18.
Note rather than raycast a bunch of rays, why not just check the angle between your forward and the player? Something like:
Vector3 v3 = player.position - transform.position;
if (Vector3.Angle(v3, headdummyBone.forward) < fovAngle / 2) {
// Sees the player
}
This will behave differently than your current code since it will also spot the player if he is below or above your fan of rays.
And a RaycastHit is a struct, so you don't have to do a 'new' to initialize it.
The problem is at this part. The break you said I must add is above it like so right? It doesn't really fix the problem but it does detect the distance now I think. Though I can tell for sure because its not stopping at else{}.
}
if(aimhit.transform.tag == "Player"){
canFollow = true;
looking = true;
target = aimhit.transform;
}else{
StopFollowing();
}
}
}
}
There's no break statement in the code you pasted in the comment. You want to add '`break;`' at line 18. Or in the code you just pulled out in the comment, it would go on line 5.
O$$anonymous$$! cool, it worked THAN$$anonymous$$S!
Thank you...
canFollow = true;
looking = true;
target = aimhit.transform;
break;
}else{
canFollow = false;
}
}
}
}