人工智能-Unity-汽车AI避障



很抱歉问得太频繁了。我对Andrew Gotow的汽车AI有一个问题。问题是AI没有避开他们前面的玩家/汽车,或者他们只是向前冲,撞到了他们前面的球员/汽车。当撞到汽车或任何障碍物时,它们停止加速或停止工作

请帮帮我。我不知道如何让AI不应该碰到障碍

这是我使用的代码。感谢的帮助

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
}

}

据我所知,你的人工智能有一个测试,看看它是否会与什么东西碰撞。该测试的结果似乎被分配给变量alternativeWaypointer,允许以下if语句:

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

你的调整轨迹逻辑可能不起作用。也许这是可以测试的?取而代之的是,一个可能的问题是,你调整后的轨迹没有得到足够的调整来避免碰撞。我们可以通过创建一系列假设轨迹,然后选择最佳候选者来解决这个问题。

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);
}

您可以使用现有的逻辑来选择一个首选轨迹。这不会是世界上最复杂的事情,但它将是一个起点:

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();
}

我把伪代码和一些java混合在一起,试图让它更清晰。这看起来能解决你的问题吗?

最新更新