- Home /
AI Object Avoidance help
I am working on making my AI avoid obstacles during space flight, but things aren't working well.
The ship is moving forward, using 'addrelativeforce' to maintain a speed of 100. I put a wall infront of the ship that I want it to avoid. Eventually I'm going to add some things to make it avoid to the left, right, up, or down, but for now I am only concentrating on the 'up'
My current workflow -
Checks to see if obstacle is in the way (using a rigidbody sweep test) about 100 units out. Normally here it would check to see if left,right,up,or down are easiest to escape, but for the purposes of testing I am only dealing with 'up.' After the test, I am trying to avoid the wall by adding relative torque so that the nose of my ship goes up.
Basically, as soon as my sweep test is true, my ship applies breaks (addrelativeforce in a negative forward direction), applies relative torque to turn 'up' and adds a forward acceleration using 'addrelativeforce' again.
This all happens in a function that is called every fixed update. Once that function returns false(aka rigidbody sweeptest fails), I call a 'straightenout' function I have that adjust the ship velocity from whatever direction it was in to forward.
EDIT -----
Here is some code. I don't have access to my actual code at the moment so this is coming from memory
var wasTurning : boolean; var status : String; var straightening : boolean = false;
function FixedUpdate(){
if(ObstacleAvoid()){ wasTurning = true; status = "evading"; }
if(!ObstacleAvoid()){ if (wasTurning){ StraightenOut(); wasTurning = false; }
}
switch(status){ case "normal": Accelerate(); TrackTarget(); break; case "evading": Accelerate(); ObstacleAvoid(); break; }
}
function Accelerate(){ rigidbody.AddRelativeForce(Vector3.forward 25); } function ObstacleAvoid(){ var bodyHit : RayCastHit; if(rigidbody.SweepTest(transform.position, Transform.forward, bodyHit, 100){ Break(); rigidbody.AddRelativeTorque(Vector3.right 10); rigidbody.AddRelativeForce(Vector3.forward * 25); } }
function Break(){ rigidbody.AddRelativeForce(-Vector3.forward * 10); }
function StraightenOut(){ straightening = true; startVelocity = rigidbody.velocity.magnitude; }
This code is obviously not EVERYTHING I have. The way straightening works is if the function is called and straighten = true, then it lerps between the old velocity to the new velocity which is equal to Transform.forward.normalized * startVelocity. This way, it takes the speed you were moving before in a certain direction, and slowly morphs it into the same velocity going forward. This is what I use to keep my ships from flying around in big circles due to momentum.
Also, TrackTarget() which is called everytime you're in status "normal" merely takes the enemy position and rotatestoward it over time so the ship faces the target. Once the target is lined up, it also calls the StraightenOut() function to make flying straight once again.
Since this is not all my code, there are other cases that set the status back to "normal" during the FixedUpdate and unless the ObstacleAvoid sets it back to "evading" the status stays at normal.
----EDIT
However, my ship doesn't smoothly avoid by going over the obstacles, and in stead does a weird slowdown while turning upward, even moves backwards for a moment, then goes forward off in a weird direction as soon as it starts trying to fly straight again. I presume this is due to the relativetorque I kept adding as it was avoiding, but I don't know a better way to do it.
Any ideas?
If you could edit your post and include your 'fixed update' function (or the relevant portions of it), that might make it easier to guess as to what the problem might be.
Sadly I do not have access to my code at the moment. I'll write up a suitable facsimile of my code and post that shortly.
I can't work on my game during work, might as well try to figure out some things via unityanswers before I get a chance to work on it again :)
I probably learned everything I know using the same strategy!
Your answer
Follow this Question
Related Questions
AI to make an object wander around and avoid obstacles? 5 Answers
Collision detection with raycast 1 Answer
Keeping an raycasting AI within a patrol area 1 Answer
Collsions problem 0 Answers