请帮帮我。我无法弄清楚如何让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
}
}
答案 0 :(得分: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混合在一起,试图让它更清晰。这看起来会解决您的问题吗?