** UPDATE BELOW **
Hello,
I’m trying to build a basic obstacle avoidance ai for a top-down shooter using ray cast. To cut a long story short, I found the rays were being cast out the enemies’ backs, not straight, but at an angle. Any help on figuring out why the ray is not being cast in the right direction would be hugely appreciated!
Here’s part of the code for the obstacle avoidance
// Get targetHeading/Direction to Target
targetHeading = targetTurretPosition - transform.position;
targetDirection = targetHeading.normalized;
// Declare RayCashHit
RaycastHit hit;
// Raycast forward from transform, to a distance of 2
if(Physics.Raycast(transform.position, transform.forward, out hit, 4f))
{
// Make sure ray is not hitting the gameobject
if(hit.transform != transform)
{
Debug.Log("EnemyScript: RayHit");
Debug.DrawLine(transform.position, hit.point, Color.red);
// Add direction from hit face, times how much force to repel by
targetDirection += hit.normal * 100;
rayhit = true;
}
}
Here’s how the enemies are being created:
GameObject go = (GameObject) Instantiate(enemy, activeSpawn, Quaternion.identity);
Please let me know if you need any more info to solve this problem!
** UPDATE 1 **
I’ve tried adding a number of empty child gameobjects around the enemy, roughly at 45 degree intervals. These are declared as public gameobjects in the script, and assigned in the inspector. There is no appreciable difference in where the ray is being cast to, regardless of which sensor the ray is directed to go.