Search Unity

AI for a 2D Game

Discussion in 'Scripting' started by panicq, Sep 3, 2015.

  1. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Hello,

    I'm creating a 2D game and I use unity's 2D features. I need to do a sort of melee fight and I've already created a simple AI, but not smart enough to QUICKLY avoid obstacles (walls or holes). So I thought about the built in navmesh but unfortunately it is only working in the XZ plane and not in the XY plane. So I can't get around the problem because all the code uses 2D features that are meant for this last coordinate system.

    Any idea ?

    Thanks in advance :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You don't need a navmesh. If you explain the structure of your game, we can probably suggest some alternative approaches.
     
  3. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Thank for the reply, here are more details:

    It's a beat them up style game (in the mood of street fighter beat'em up) , where enemies are moving toward the player and start attacking when they are near him. The player can move anywhere. Also I should say that we want to manage more than 20 enemies at the same time in a melee (that's why I wanted to use a navmesh, which I did use in an other game of the same kind, but in 3D). For now when an enemy collides with another one, they automatically choose a random direction and move during 0.5 sec without focusing the player. There is also a knock back force applied on the enemy when the player hits him. Concerning the other elements, we have some obstacles such as walls or holes that they need to avoid. And for now I tried different approaches when an enemy collides with one of them such as calculating a vector in the opposite direction in a range from -90 to 90 deg or moving up or down in order to slide along the surface of the wall but it's not really relevant. I also tried something with raycasts but same thing it's not really optimum :(
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    So you're not using any (even 2D) physics? And if not, why not?
     
  5. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Yes, I'm using the 2D Physics engine in order to detect collisions between the enemies. But in which way it will help me solving my obstacle avoidance problem ? Because if I just let the physic simulation as it is without any avoidance logic, enemies will be stuck into the wall, or simply won't try to get around the obstacles. That's the reason why I thought about navmeshs :) What would you do in this situation ?

    Thanks for your reply by the way :)
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Check out steering behaviours. Steering behaviours are your low level AI layer that deals with collision and obstacle avoidance.

    Below this layer you have the motor layer, that just moves the player about. Above that layer you have path finding and goal planning.

    Layers are awesome for simplifying AI and making it into smaller, more digestible chunks.
     
    Korno likes this.
  7. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    Thank you for your answer :)

    Unfortunately this is not giving me clues in how I can code an "avoidance behavior" without a navmesh :(
    By the way, layering the steps of an AI is a nice architecture and that's what I've done in some kind until here.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well of course you still need some sort of data structure representing where the agents can and can't go. Though it's possible you can just use the physics engine and raycasting, unless you really do have a maze-like environment with dead ends.

    Can you post a screen shot of your game, and point out what the obstacles are? It matters very much whether they are blocks on a grid (like classic Nintendo platformers for example), or axis-aligned boxes, or arbitrary polygons, or circles.
     
    Kiwasi likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Did you check out steering behaviours?