Strange Raycast problem
Hey guys I have problem with my raycasts,
I have a enemy which search his position in the near of the player and then shoot at him. The most of the time it works even with multiple enemys of this type. But sometimes if more than one of these move in the same areal they stuck without no reason. They didn't have touch each other and it happens just with single enemys.
The problem is the raycast, which checks if the possible position is reachable or if a collider is in the way. Then they stuck, this ray says all poistion arent accessabile because there is a collider in the way, even if there is nothing in a mile.
So I printed a Debug.Log with the tag and the instance ID of the collider which blocks the way. Each time the collider has the tag "Enemy" and Instance ID is something which either didn't exist in the whole game or its something like the animator component of one the other enemys o.O? Even if I delete all enemys, expect the one with the error, from the scene (In playmode of course) there is still a enemy which blocks his way :(
So enough words for now, here is my code:
void SearchForPositions() {
atkPos1 = new Vector3 (target.transform.position.x, target.transform.position.y - ShotRange, 0);
atkPos2 = new Vector3 (target.transform.position.x, target.transform.position.y + ShotRange, 0);
atkPos3 = new Vector3 (target.transform.position.x - ShotRange, target.transform.position.y, 0);
atkPos4 = new Vector3 (target.transform.position.x + ShotRange, target.transform.position.y, 0);
accessiblePos1 = checkAccessiblePos (transform.position, atkPos1);
accessiblePos2 = checkAccessiblePos (transform.position, atkPos2);
accessiblePos3 = checkAccessiblePos (transform.position, atkPos3);
accessiblePos4 = checkAccessiblePos (transform.position, atkPos4);
}
bool checkAccessiblePos(Vector3 startPos, Vector3 endPos) {
colliderSize = myCollider.size;
findRay = new Ray2D(transform.position, (endPos - startPos).normalized);
findRayHit = Physics2D.CircleCast(findRay.origin, colliderSize.x, findRay.direction, Vector2.Distance(startPos,endPos), layerMask);
if (findRayHit.collider == null) {
return true;
} else {
return false;
}
}
I'm working one this problem for at least 20 hours but I'm stuck like my enemy ^^'
It would be really great if someone could help me out of this :)
Are you using a capsule collider as your hitbox? I've had problems where raycasts hit my AI's hitboxes (it was especially problematic with shooting). You may want to mask out your enemies from the raycast, and see if that helps. Plus, if you're looking to see if you can get somewhere, you're hardly going to notice the guy in your way.
Hey Taorcb thanks for the reply^^
At first I use BoxCollider2D for my enemys. Sadly I need the enemy layer in the raycast. Without it they collide against each other the whole time. But for testing I masked them now out.
Ok now it looks like the error didn't appears o.o
I don't get it^^ Is something wrong the way I send the raycast?
Oh and I turned the option "Raycasts start in Collider" in the projectsettings to off, so I don't think they hit themself. Especially because they work perfect then there is just 1 of the enemys.
================ EDIT: Ok now I got something: When they're stuck and I'm disable there own Boxcollider they can move again. So do the ray really hit themself o.o? Btw. another way to unstuck them is to move them a little bit.
Your answer
Follow this Question
Related Questions
Need help making a ledge climber. The problem, transform.position always returns to vector( 0, 0) 0 Answers
Tag doesn't work when gameObject is a child of the Main camera? 1 Answer
How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers