- Home /
Unity AI Obstacle Avoidance Help
Ok I have an AI script that works (so far), except for the obstacle avoidance. What I want is, when one of the raycast hits an object for it to turn and then continue to head towards the target (which may turn from the next waypoint to the player). The problem is that the enemy jitters in a certain direction and then tries to go to the waypoint. Also is there anyway I can write this in less code? I'm pretty noob at complex stuff like this, the collision detection was horribly written but sort of works aside from the jittering instead of turning and then trying to go to the waypoint. Here is the code, it's very long:
var rotationSpeed = 5;
var moveSpeed = 5;
var maxHealth = 100.0;
var curHealth = 100.0;
var healthBarLength : float;
var waypoint : GameObject[];
var currentWaypoint : int;
var maxWaypoint : int;
var target : GameObject;
var startingDrag : float;
var groundDistance : float;
var isGrouned : boolean;
var isObstructed : boolean;
function Start(){
healthBarLength = Screen.width / 4;
distToGround = collider.bounds.extents.y;
waypoint = GameObject.FindGameObjectsWithTag ("Waypoint");
System.Array.Reverse(waypoint);
startingDrag = rigidbody.drag;
isObstructed = false;
}
function Update(){
target = waypoint[currentWaypoint];
var fwd = transform.TransformDirection (Vector3.forward);
var l25D = Quaternion.Euler(0,-25,0) * transform.forward;
var r25D = Quaternion.Euler(0,25,0) * transform.forward;
var l50D = Quaternion.Euler(0,-50,0) * transform.forward;
var r50D = Quaternion.Euler(0,50,0) * transform.forward;
var left1 = Quaternion.LookRotation(l25D);
var right1 = Quaternion.LookRotation(r25D);
var left2 = Quaternion.LookRotation(l50D);
var right2 = Quaternion.LookRotation(r50D);
var targetPos = target.transform.position;
var targetRotation = Quaternion.LookRotation(targetPos - transform.position);
var i = Time.deltaTime * rotationSpeed;
var m = Time.deltaTime * moveSpeed;
var relativePos = targetPos - transform.position;
if(isObstructed == false){
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, i);
rigidbody.AddForce(m * relativePos.normalized);
}
if(currentWaypoint >= maxWaypoint){
currentWaypoint = 0;
}
if (Physics.Raycast (transform.position, fwd, 3)) {
}
if (Physics.Raycast (transform.position, l25D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, right2, i);
rigidbody.AddForce(Vector3.right2 * m);
}else
Invoke ("turn", 4);
if (Physics.Raycast (transform.position, l50D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, right1, i);
rigidbody.AddForce(Vector3.right1 * m);
}else
Invoke ("turn", 4);
if (Physics.Raycast (transform.position, r25D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, left2, i);
rigidbody.AddForce(Vector3.left2 * m);
}else
Invoke ("turn", 4);
if (Physics.Raycast (transform.position, r50D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, left1, i);
rigidbody.AddForce(Vector3.left1 * m);
}else
Invoke ("turn", 4);
}
function Health() {
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 4) * (curHealth / maxHealth);
}
function OnGUI() {
GUI.Box(Rect(1050, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
function OnDrawGizmos () {
var fwd = transform.TransformDirection (Vector3.forward);
var l25D = Quaternion.Euler(0,-25,0) * transform.forward;
var r25D = Quaternion.Euler(0,25,0) * transform.forward;
var l50D = Quaternion.Euler(0,-50,0) * transform.forward;
var r50D = Quaternion.Euler(0,50,0) * transform.forward;
// Draws a 5 meter long red line in front of the object
Gizmos.color = Color.red;
var direction : Vector3 = transform.TransformDirection (Vector3.forward) * 3;
var direction2 : Vector3 = l25D * 3;
var direction3 : Vector3 = l50D * 3;
var direction4 : Vector3 = r25D * 3;
var direction5 : Vector3 = r50D * 3;
Gizmos.DrawRay (transform.position, direction);
Gizmos.DrawRay (transform.position, direction2);
Gizmos.DrawRay (transform.position, direction3);
Gizmos.DrawRay (transform.position, direction4);
Gizmos.DrawRay (transform.position, direction5);
}
function OnTriggerEnter(info : Collider){
if(info.tag == "Waypoint"){
currentWaypoint++;
yield slow();
}
}
function slow(){
rigidbody.drag = 100;
yield WaitForSeconds(.1);
rigidbody.drag = startingDrag;
}
function turn(){
isObstructed = false;
}
Answer by csetter · Aug 18, 2012 at 03:59 PM
I'm doing something similar and ran into the same problem. To stop the jittering, you need to stop the your game object from trying to head to your next waypoint, until it's passed the obstacle.
I did this by creating a temporary waypoint at the colission point, and then moving the temporary waypoint along the z axis until the raycast hits stopped.
You need to store the original waypoint you're heading too, and then re-establish it once you reach the temporary waypoint, and then destroy the temporary waypoint.
You could also add some velocity retardation based on distance if you needed too.
Hope this helps (took me a few nights to work this out)
Chris
Answer by lyzard · May 27, 2012 at 10:08 AM
I think I had a similar problem with my NPC. For collision avoidance, I looked at the tangent points of the obstacle to define the new target and what happened is that for 1 frame direction was corrected by the collision avoidance, but the frame after, the path was clear and the direction was setup by the next waypoint, however while moving the NPC, the next frame made it necessary to correct again. So it alternated between a waypoint target and a fixed target, making the character to jitter. What I did was simply smoothing the current rotation to the expected rotation, hence reducing the jitter, with something like:
rigidbody.MoveRotation(Quaternion.Slerp(rigidbody.rotation, Quaternion.LookRotation(look), 0.2f));
It's not perfect though, but it's a first step in the right direction I think.
Answer by GC1983 · May 27, 2012 at 02:40 AM
One name....SimplePath.
Really? Youre going to give me negative karma for suggesting something that will make your life so much easier than writing 1000 lines of uncertain object avoidance code?....
I don't see misinformation or something that is wrong in this answer. Well it's not that comprehensive, but nothing that would explain a downvote. If you downvote a post, give at least a reason in a comment. Voting is nothing personal here. This is like a wiki to collect knowledge for the whole community.
I'll add at least a link to increase the value of this answer ;)
Your answer
Follow this Question
Related Questions
Follow AI which avoides obstacles 1 Answer
How can I make an AI that avoids obstacles WITHOUT using NavMesh? 3 Answers
Raycast Steering Problem 1 Answer
Script is Flipping Out! 2 Answers
Enemy Rotation AI 0 Answers