Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

AI for a 2D shooter

Discussion in 'Scripting' started by blaze, Aug 3, 2012.

  1. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    Hiho!

    I'm working in a 2D Shooter (x, y). Like Contra (http://goo.gl/4FxBB) where, will be like a Arena, where who kills more, more points earn.
    Then, my AI, needs to explore the map (run, jump, fly(with jetpack)), attack the player (get near, aim, shot), and protect itself (run to the hills, take cover).

    For now, I'm working in the patrol system, where I put some waypoints and the character goes there. But, I dont know if waypoints is the right way to walk in the map.

    Then, someone can help me, give some tips about what kind of pathfind I can use? Would be waypoints a good way? Or, I would need to create something more "heavy"?

    The target is iOS/Android, then, can not be a x64 algorithm. :)

    My objective is something like this: http://goo.gl/uQPWd
     
  2. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    If you want to make it in a stable and performance wise cheap way I'd suggest sticking to waypoints. You can do per level Waypoints that the ai is going to be walking along. The Main problems that you need to cover are dependent on the terrain. You first of all need to write down all cases that can occur, and then what the solution would be. Once this is done you can start implementing.

    Example: The AI_guy's y-axis value is 20 points below the targetwaypoints y-value AND the AI_guy's x-axis value is below 20 units to target waypoints x-value, then the AI_guy needs to jump towards the target as there is a big height difference and he's near enough to reach the slope or cube or whatever.

    When a player is nearby the AI_guy might want to follow him depending on his attackrange, when the player is far away again the AI_guy needs to again move towards his previous target waypoint.

    In order to find out when the AI_guy has to jump can be solved through checking the change of the height value change according to the target waypoints height.

    Let's say the AI_guy walks on a slope towards his target, in that case there is no need for him to jump, the indication is that his height is increasing steady towards target height. When the character walks a while towards his target and the height difference does not seem to change depending on your level design he has to make the decision to jump towards the target.


    Another approach could be that you use Box colliders which once the AI_guy enters them tell him to jump at that area
     
  3. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    Thank you very much for the suggestions!

    The box collider for the jumps is what I will use. I was thinking in something similar, but I would make Waypoints for jump, not thought about Triggers. Thanks again!

    I picked a FSM script in somewhere, and is working very well. For now, I was working in the patrol state. And is finished, not 100% but is working.
    Here is what I created: http://goo.gl/e7dvu
    Red balls are waypoints, blue cubes are jump triggers.

    Now, I'm thinking, when the bot spot the player, it will run against the player. Then, if he kills the player, he will get back to PatrolState. If it was in one waypoint, and he go back to that waypoint, will be so weird.
    Then, how can I make the bot get the nearest waypoint to start following it?
    Will I need to look every waypoint to see what is the nearest, or there is another good way?

    Another question, the arena will not be so big. Then, the bot needs to walk but randomly, not walking always in the same way.
    Then, I thought, if I generate random a number, then make the bot go to that waypoint, it would be interesting, but, how would I do to the bot know the "path" to that waypoint?
    I need to show him, that he needs to go here, here, and here, to then arrive that waypoint. How can I implement this? This will be something like pathfinding.

    Sorry for the bad english! ;)
     
  4. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    About the jump triggers, how can I do, to configurate to from to what direction the bot will jump?
    Explaning, when the bot comes from left, it will jump, but when it is coming from the right, it ignores the trigger.
    This need to be configured in the trigger. Because, each trigger need a different configuration.

    Thanks.
     
  5. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    About the nearest waypoint thing. you need a generic list of type collider. This is how it works.

    when the bot kills the player, an invisible huge trigger collider (sphere) will be around the bot and does add all surrounding waypoint colliders into his generic list. once this is done, you go through the list in a for loop checking the distance of all available lists waypoints for the nearest with pythagoras, once found set the bots target waypoint to the nearest.

    edit: for the jumping and the colliders you just need to figure out whether X-axis speed of the bot is positive or negative to determine the action. just drop a script on the jumpcollider object that contains direction dependent actions and sends them via (use ontriggerenter and find out whether the collider is tagged as bot and then reference tell the actions via public functions of the bot or messaging) reference to the bot.
     
    Last edited: Aug 5, 2012
  6. blaze

    blaze

    Joined:
    Dec 21, 2011
    Posts:
    211
    I'm back to the game's AI now. I cant use triggers for the AI, because I'm planning to make a Level Editor in the game.
    I need that the sticks run alone, without none attached in the map. Just like it see something.

    I will need something like this:
    $see.png

    I'm thinking in use linecasts to get the "space" in the front of the bot.

    1) He see that is a obstacle in his front, and the upper linecast see that there is no collision above, then he can try to jump.
    2) The same thing, his see the obstacle, and knows that he can jump and will not collide with a wall.
    3) Now he will get that there is a cliff, and if he walks forward he can die.

    I think that this is a good place for the "sensors", but how I could make he knows the entire level, to know where he can drop without die, or, to know if he jumps, will not get out of the map and drop in the void.

    No one knows any technics another than "waypoints and triggers"? I cant found any documentation about this kind of thing in the internet. :-|
     
  7. Myhijim

    Myhijim

    Joined:
    Jun 15, 2012
    Posts:
    1,148
    You could add two invisble points to the left and right of the character, which send raycasts straight down checking if it is within the possible, non-leathal drop. Using a "Sensor" In a way

     
    Last edited: Sep 25, 2012
  8. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    I did something like this when having to make AI do parkour in UDK. I would have a 3 tracers, one at the feet, waist and about a meter above the head. Once the feet and waist were not null(touched something), but the head was null, the AI jumps.
    Basically, everything you have to make the player move, jump, run, etc. make it in a function that can be called by the AI once conditions are met.
     
unityunity