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