- Home /
Trigger-based vehicle ai?
Is it feasible to create an AI system using triggers to detect objects? If not, why? If yes, would there be any major drawbacks?
Answer by eeveelution8 · Jun 13, 2015 at 06:18 AM
It largely depends on what kind of game you are making, if were a vehicle combat game kind of like Twisted metal where the cars would insted become hostile after triggering them, a good way to start would be having large trigger colliders placed where you want the car to become hostile. Have all the coding for the car done, but have it only activate when a condition (for example a boolean) is met. Once it has, the cars AI will activate and try to get you. The trigger boxes can be set to make the cars condition met once entered, then delete themself.
If it on the othe hand is a scripted AI that appears when the player enters a street for example, the same kind of trigger box can utilise the SetActive(true); command, while having the cars AI premade to activate on the Start function, initially set to be inactive.
If it's a racing game, I don't see how triggers will come into play.
Actually, a racing game is exactly what I'm making. I'm thinking of using triggers attached to the ai cars so they know when to brake (ie a traffic car enters the trigger, so they slow down), and also steer. Also, police cars might use trigger so they know that they are in range of a suspect to stop an arrest them.
If you are doing a racing game, try having all the behaviours your cars can do pre-coded, but activated when they enter specific triggers setup at traffic lights, crosswalks, etc.. Just make sure to add sections in your code that make the AI know to tart moving again after having been triggered to slow down, and speed up.
As for cop cars, You may want to try having them have a large hit box trigger within them, and when the player enters the space, the cops go hostile and chase you.
Actually, what I meant was triggers for the actual cop chasing part, (ie if player is in trigger one, drive left, if he/she is in trigger two, drive right), but the other uses do sound like they could work well.
for that, it may be a little harder, if you want to make it organic and sandboxy, I don't know how to help you, but if it's linear, and the roads are preset, having the trigger be set to tell the cop to turn to keep going would work. but this requires there to be a set number of paths to make.
If you are going the sandbox route where the areas you can drive are anywhere, try looking to navmeshes and nav agents, since that amount of fiddlyness with triggers would be a worse use of your time than to learn navmeshes.
Unfortunately, I can't use a navmesh or navagent because the cars are controlled by wheelcolliders. You did answer my question, though, so thanks.
Your answer
Follow this Question
Related Questions
Use different colliders 3 Answers
Can't click gameobject when over another trigger? 1 Answer
How to Specify a Collider in OnTriggerStay function? 1 Answer
Multiple OnTriggerEnter in one script 1 Answer
changing gravity OnTriggerEnter 1 Answer