Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make AI?

Discussion in 'Scripting' started by ItsKhanny, Jan 14, 2016.

  1. ItsKhanny

    ItsKhanny

    Joined:
    Feb 4, 2015
    Posts:
    30
    Ive been directed to the forums for my question I had on unity answers, But here it is. How do I create an AI that runs around in a panicked fashion. I want them to just run around all panicked and I dont know how to start. Who ever helps I want to say thank you
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Right, well whether you need a NavMesh is utterly dependent on what sort of game you're making. I haven't had opportunity yet to use them myself, but I know they're great for certain game types.

    Can you tell us more about your game? 2D or 3D? Turn-based or real time? Physics engine, or no? What's in the environment? Or post a screenshot, which is worth a thousand words.

    Assuming the simplest case (your panicked agents are cubes running around in an infinite plane with no obstacles or chance of colliding with each other), then I would just pick a random direction, move that way for a short while, and then pick another random direction.

    This reminds me of a game I wrote years ago, called Zombies! It was a turn-based strategy game, and the human units had three states: Clueless, Panicked, and Aware. The Panicked units would throw their hands up and run around at random — sometimes right into the hungry jaws of a zombie! You can just barely see one guy panicking on the left edge of the image below.

     
  4. ItsKhanny

    ItsKhanny

    Joined:
    Feb 4, 2015
    Posts:
    30
    I got it, I learned nav mesh and I used it to create it to where they run to their house, But to answer your questions JoeStrout, 3D real time without the physics environment.
    Edit: Also I am trying to make it to where the AI will run around in a panicked fashion because there is an alien in my game

    This is the screenshot of what I have
    Screen Shot 2016-01-14 at 1.11.46 PM.png Screen Shot 2016-01-14 at 1.11.57 PM.png
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    OK, in that case I'd go with something like @Munchy2007's suggestion, using Random.InsideUnitCircle to pick a random nearby point, use the navmesh to run to it, and then repeat.
     
  6. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Be careful not to recalculate paths a lot. It's like, the most expensive thing ever...
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Expensive is relative, and may not necessarily be a problem. I wrote a multiplayer FPS that had AI controlled zombies following random paths on a navmesh and it ran on a PC without any problems with over 100 zombies in the level. (That was a stress test though, ordinarily it would have 10-20 zombies). They recalculated their paths randomly every 2-10 seconds.

    As I recall when I set the zombies to spawn endlessly it crashed the game at around 500 of them.
     
    JoeStrout likes this.
  8. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    It's not relative at all. As you said, they only updated once every 2-10 seconds. You're doing it right. Now, if you were a noob, you'd likely have that recalculate in Update() or something. How often you call something doesn't change the cost of the call. As I said... it's expensive... so don't use it a lot. :)
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For some more advanced stuff check out the book 'game so by example' by m Buckland.