[英]What would be the most effective way to have my automated vehicle's behavior change every time it reached a new waypoint? (Python)
I would like to have my automated vehicle change its behavior every time it reaches a new waypoint.我想让我的自动驾驶汽车在每次到达一个新的航路点时改变它的行为。 I planned to nest "if" statements for however many iterations I end up choosing, but the loop always gets stuck in the first if statement, keeping the vehicle in "aggressive" every time it reaches a new waypoint.
我计划为最终选择的多次迭代嵌套“if”语句,但循环总是卡在第一个 if 语句中,每次到达新航点时都保持车辆“积极”。 What would be the best way to ensure the behavior changes everytime it reaches a new waypoint?
确保每次到达新航点时行为都会发生变化的最佳方法是什么?
if agent.done():
agent = BehaviorAgent(world.player, behavior='aggressive')
agent.set_destination(random.choice(spawn_points).location)
world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
print("The target has been reached, searching for another target")
if agent.done():
agent = BehaviorAgent(world.player, behavior='cautious')
agent.set_destination(random.choice(spawn_points).location)
world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
print("The target has been reached, searching for another target")
Please let me know if any additional code is needed.如果需要任何其他代码,请告诉我。
It is possible that I completely misunderstood the question, but if I understood right, you want to change it every time a destination from aggressive to cautious and back to aggressive.我可能完全误解了这个问题,但如果我理解正确,您希望每次将目的地从激进变为谨慎,然后再回到激进。 I would do it with a global variable like this:
我会用这样的全局变量来做到这一点:
current_behavior = "cautious"
if agent.done():
if current_behavior == "cautious":
current_behavior = "aggressive"
agent = BehaviorAgent(world.player, behavior='aggressive')
agent.set_destination(random.choice(spawn_points).location)
world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
print("The target has been reached, searching for another target")
elif current_behavior == "aggressive":
current_behavior = "cautious"
agent = BehaviorAgent(world.player, behavior='cautious')
agent.set_destination(random.choice(spawn_points).location)
world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
print("The target has been reached, searching for another target")
And I would also clean up the unnecessary code like this:我也会像这样清理不必要的代码:
current_behavior = "cautious"
if agent.done():
if current_behavior == "cautious":
agent = BehaviorAgent(world.player, behavior=current_behavior)
current_behavior = "aggressive"
elif current_behavior == "aggressive":
agent = BehaviorAgent(world.player, behavior=current_behavior)
current_behavior = "cautious"
agent.set_destination(random.choice(spawn_points).location)
world.hud.notification("The target has been reached, searching for another target", seconds=4.0)
print("The target has been reached, searching for another target")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.