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

RTS AI FSM, Handling Multiple Enemy Units

Discussion in 'Scripting' started by moffata2, Mar 11, 2016.

  1. moffata2

    moffata2

    Joined:
    Jun 17, 2015
    Posts:
    25
    Hi,

    I am creating an RTS and for my units and enemies I am implementing State Machine design like in this tutorial

    http://unity3d.com/learn/tutorials/...rchive/state-machine-interface?playlist=17117

    My problem with both player and enemy state machines seems to be around multiple units in range. For one unit I am using trigger colliders to detect an enemy in range and transitioning states accordingly. Is this the best way to do it. I am thinking about holding a List<GameObject> enemies and filling and removing from the list with OnTriggerEnter and OnTriggerExit has anyone used this way? Or found a better way?


    Warm regards,

    Tony
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I've just skimmed through this video. The title of this course is "Introduction to State Machines". Maybe it's just me, and watching the video entirely at a normal pace would make more sens :)... In my opinion, this is not a good material if you want the learn about the fundamentals of FSM. I found this video rather confusing.

    Maybe I simply missed it, but the two import pieces of information about any FSM are:
    - What are the states.
    - What are the transitions to go from one state to another.

    Independently of how your FSM is implemented ( visually or in code ), this information should be clearly available and readable. Otherwise, it does not make sens to use a state machine.
     
    moffata2 and Kiwasi like this.
  3. moffata2

    moffata2

    Joined:
    Jun 17, 2015
    Posts:
    25

    Thanks for your reply, Every state implements the same interface which defines all the transitions and calls update on the current state. My transitions are then called according to events/conditions that are checked in the update. The one in the video also calls ontriggerenter for the current state passing the collider but I have removed this and just hold and remove the units in a list at the top level which can be used and check in any state in the update and handle the transitions accordingly. I have resevations about tracking units in this way though!
     
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    You're mention different things in your post. I don't quiet understand what you are asking exactly. Do you have an issue about using an FSM or something else?

    About keeping a list of game objects currently colliding, I think is a good idea to update this list OnTriggerEnter and OnTriggerExit.
     
    moffata2 likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Yes this is a sensible way of keeping track of how many things are in range. It also allows you to do graphics things such as highlight the enemies in range etc.

    An alternative would be to use a overlapshere check at the time when you want to make the decision, but that might limit what you can do with the information.
     
    moffata2 likes this.
  6. moffata2

    moffata2

    Joined:
    Jun 17, 2015
    Posts:
    25
    Thanks, yeah just wanted a bit of validation around the approach I'm taking before I implement all my units... this is my third time rewriting it.
     
  7. moffata2

    moffata2

    Joined:
    Jun 17, 2015
    Posts:
    25

    Thanks yeah I'm a bit of a newbie at unity....not programming! Have not used overlap sphere but I'll have a look before I proceed thanks! Can you get a reference to the gameobjects?
     
  8. Matthew-Schell

    Matthew-Schell

    Unity Technologies

    Joined:
    Oct 1, 2014
    Posts:
    238
    I would consider looking at Physics.OverlapSphere as well. It may be a little more performant (depending on how your game is set up) and you can control how often it checks using a coroutine. Without knowing a lot more about your game it's hard to offer much more. I would keep going the way you're going if it's performant and works though.
     
    moffata2 likes this.
  9. moffata2

    moffata2

    Joined:
    Jun 17, 2015
    Posts:
    25
    Great! Thanks Matt I'll send you a copy of my first beta :)
     
    Matthew-Schell likes this.