- Home /
Obstacle avoidance not working
First of all, I've already tried every free obstacle avoidance and pathfinding tool there is. Yes, every single one. NONE of them work for me. Except for Path, which I never even downloaded due to the sheer number of reviews informing others that it completely ruins your scene and forces you to rebuild from scratch. I've also tried EVERY SINGLE obstacle avoidance and pathfinding script I could find after searching 2 days for a solution. There is simply no script on the entire internet that will work for me. I've searched google, believe me, going so far that the search results don't even have anything to do with Unity anymore. Still, no luck. I hope you can understand my practically painful frustration. First, I'll start with one of the countless number of scripts that I've already tried.
var target : Transform;
var repelForce : int = 20; // The force of which the object is pushed
var walkSpeed : float = 1;
function Update(){
var dir = (target.position - transform.position).normalized;
var hit : RaycastHit;
var leftRay = transform.position;
var rightRay = transform.position;
leftRay.x -= 2;
rightRay.x += 2;
if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
if(hit.transform != transform){
Debug.DrawLine(transform.position, hit.point, Color.red);
dir += hit.normal * repelForce;
}
}
if(Physics.Raycast(leftRay, transform.forward, hit, 20)){
if(hit.transform != transform){
Debug.DrawLine(leftRay, hit.point, Color.red);
dir += hit.normal * repelForce;
}
}
if(Physics.Raycast(rightRay, transform.forward, hit, 20)){
if(hit.transform != transform){
Debug.DrawLine(rightRay, hit.point, Color.red);
dir += hit.normal * repelForce;
}
}
var rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * walkSpeed * Time.deltaTime;
}
What's going wrong? Well, everything. My enemy isn't even going after the player. It's running off into other parts of the level, while twitching like crazy and leaning about 45 degrees to the right...? After a minute of wandering, it starts going in circles, then starts going through walls. Not to mention the fact that it's floating about 3 feet off the ground the entire time. That is until it decides to fly diagonally up into space. So I guess what I'm asking is if there are any other working object avoidance scripts I could try. I should also mention that I'm NOT using a flat level with simple primitives here and there. I'm using a very large and complex building with multiple levels. So box, sphere and capsule colliders will not work for me, unless I want to spend 5 days covering every single wall and stairway with them. I've tried different scripts that would have worked, if my building was using box colliders instead of mesh colliders. I don't know if raycasts just don't work with mesh colliders or what, but my enemy doesn't even detect them, and goes right through them. I am extremely frustrated, and about to give up all together, but I'm clinging onto the one hope that someone here might be able to help.
Can someone please help me? I really need to figure out a way to do this.