Search Unity

Question I'm having issues with using Navmesh for sidescrolling beat-em-up AI, mostly close-up behavior.

Discussion in 'Navigation' started by Th0masThe0bvious, Feb 27, 2023.

  1. Th0masThe0bvious

    Th0masThe0bvious

    Joined:
    Aug 22, 2018
    Posts:
    80
    I am assuming many people here know what sort of game I'm describing. It's the Double Dragon sort of game where characters move in a 3D or pseudo-3D environment (mine is truly 3D, though), with the characters able to move in all four cardinal directions, but their sprites always unrealistically face to the east or the west. That brings us to a somewhat unique challenge posed by this sort of game.

    In a many 3D games with melee, switching between an approaching state and an attacking state can be a simple matter of checking whether an AI character is close enough to its target. However, that alone does not work in this sort, because characters can only attack in eastward or westward directions, necessitating a means for them getting into position to do so.

    I attempted to do this in a quick but probably less-than-ideal way. First, I gave my player character two "strike zone" child objects, which are trigger box colliders positioned on his right and left. Then, in the enemy characters' AI script, I added some states. At a great distance, it functions the way a normal 3D Navmesh AI would, going to the player's position. When it's within ten units of the player, though, it switches to a new state. In that next state, it checks which of the two strike zones is closer and sets the closer one to being its destination. Finally, an OnTriggerEnter void occurs when the bot touches one of those strike zones, it sets the Bool "inPosition" to true, (an OnTriggerExit void is also there to set that to false) and it enters a new state where it selects an attack, does the attack, then checks whether inPosition is true, which if so selects another attack, or if false, returns to the state where it chases the strike zone again.

    This all mostly works, but it's finicky. The biggest problem seems to be that "inPosition" bool; sometimes it will fail to shut off if my player character moves out of range--in other words, OnTriggerExit doesn't always seem to work--and other times it seems to shut off even if the player character is still in range. There's a fail-safe measure to detect if the player character has moved a far distance away and turn off the bool if so, but it's still fairly easy to cheese this AI if you its limits, and sometimes it just breaks in a way that would be noticeable even to people who don't. It's also not easy to strike a good balance between my strike zones' size and the enemy NavMesh agents' stopping distance; I don't want them to phase halfway through my player character before attacking (or if they're set to collide with each other, knock the player away), but I also don't want them to stop moving and attack while still out of range.

    So I don't know if anyone can tell me what features are causing all of these issues, especially without examining the file themselves (which I might let them), but I'm open to suggestions of how to implement this sort of positioned attacking in a way that doesn't utilize those strike zones and trigger collisions. I know that there are games of this sort made with Unity, such as River City Girls, so some people have clearly figured this one out.