-1

抱歉问得太频繁了。我对 Andrew Gotow 的汽车 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
}

}

4

1 回答 1

0

据我所知,你的人工智能有一个测试,看看它是否会与什么东西发生碰撞。此测试的结果似乎已分配给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 混合在一起,以使其更清晰。这看起来会解决您的问题吗?

于 2013-02-24T14:19:24.867 回答