-1

sorry for asking too often. I have a problem on Andrew Gotow's Car AI. The Problem is The AI did not avoid player / car in front of them or they just go ahead and hit the player / car in front of them. When hit a car or any obstacle, They stop accelerating or stop working.

Please help me. I can't figure it out how to make the AI shouldn't hit the obstacle

this is the code i use. Thanks for any help

function GotNewWaypoint(newWay : Vector3, newRot : Quaternion){
    wpObj.transform.position = newWay;
    wpObj.transform.rotation = newRot;// wpObj is a triggered box collider that sets GotNewWaypoint function everytime AI hits the collider
}

function NavigateTowardsWaypoint(){ // Runs in Update function
var relativePosition : Vector3;
var hit : RaycastHit;

if(!alternativeWaypointer){
    relativePosition = transform.InverseTransformPoint(Vector3(wpObj.transform.position.x,transform.position.y,wpObj.transform.position.z));
}else{// turn on alternative position if Spherecast hits object
    relativePosition = transform.InverseTransformPoint(Vector3(raycaster*wpObj.transform.position.x,transform.position.y,wpObj.transform.position.z)); // move wpObj on axis x a bit so the AI shouldn't hit the obstacle
}

if(Physics.SphereCast(transform.position,40,transform.forward,hit,30)){
    if(hit.transform != this.transform){
        if(hit.collider.tag == "Player" || hit.collider.tag == "Opponent" || hit.collider.tag == "Environtment"){
            alternativeWaypointer = true;
            raycaster = -Vector3.Normalize(hit.point).x * 5;
        }else alternativeWaypointer = false;
    }
}

inputSteer = relativePosition.x / relativePosition.magnitude;//used for ai's handling control. usually it's 1 or -1

if(Mathf.Abs(inputSteer) < .5){
    inputTorque = relativePosition.z / relativePosition.magnitude; //corner is not too tight, accelerate
}else{
    inputTorque = 0.0; // corner is too tight, stop accelerating
}

}

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • 1
    It's difficult to understand your code at a glance. Can you annotate it with some comments? Presumably, somewhere, you are trying to anticipate a collision by adjusting course if the probability of a collision reaches a certain thresh-hold. Where is this in your code? – deau Feb 24 '13 at 11:26
  • @deau i already add a comments. This code is attached in AI Car's root. I'm trying to move the waypoint a bit in x axis (wpObj) everytime the SphereCast hits something, so the AI shouldn't hit any obstacle in front of them. But they just hit obstacles in front of them anyway. But sometimes it works, the AI didn't hit each other (if the obstacles in their side) – Skipper Vértex Centurion Feb 24 '13 at 12:35

1 Answers1

0

From what I can gather, your AI has a test to see if it will collide with something. The result of this test appears to be assigned to the variable alternativeWaypointer allowing for the following if statement:

if(!alternativeWaypointer){
    relativePosition = computePosition(trajectory);
}else{
    relativePosition = computePosition(adjustTrajectory(trajectory));
}

It's possible that your adjust trajectory logic isn't working. Perhaps this can be tested? In lieu of this, a likely problem is that your adjusted trajectory isn't sufficiently adjusted to avoid the collision. We can solve this by creating a series of hypothetical trajectories and then picking the best candidate.

alternativeWaypointer = isCollisionLikely(trajectory);

if(!alternativeWaypointer) {
    relativePosition = computePosition(trajectory);
} else {
    /* generate alternative trajectories, adjustments range from minimal to extreme */
    List<Trajectory> alternatives = adjustTrajectoryList(trajectory, MINIMAL, EXTREME);

    /* Of these, pick one and run with it */
    Trajectory preferred = leastLikelyCollisionTrajectory(alternatives);
    relativePosition = computePosition(preferred);
}

You could use your existing logic to pick a preferred trajectory. It won't be the most sophisticated thing in the world, but it would be a starting point:

Trajectory leastLikelyCollistionTrajectory(List<Trajectory> options) {

    /* iterate from minimal adjustment to extreme */
    foreach(Trajectory t in options) {
        if(!isCollisionLikely(t)) {
            return t; /* return the first trajectory that seems ok */
        }
    }

    /* otherwise resort to the most extreme and hope */
    return options.last();
}

I've mixed pseudo code with a bit of java in an attempt to make it clearer. Does this look like it would solve your problem?

deau
  • 1,193
  • 1
  • 9
  • 14
  • I think i solved it. I set the input torque to zero if the ai hits any obstacle so the AI brakes if they see an obstacle in front of them or in their sides. thanks for your answer – Skipper Vértex Centurion Feb 25 '13 at 10:44