Attack action

Hi,

I’ve implemented attacking in my game but was wondering how you guys go about making a character in your platform game
run and attack the player?

I do it two ways - I use raycast from the bad guy to the player, if they can see them, they head towards them and then start attacking them,
other way is, if player is in close proximity they attack that way. I do all this via a simple state machine.

Thanks

Check out feedback loops… A good article here page 18.

I’m not sure if you’re talking about deciding when the enemy should attack or how they should get there, but for the former you could use Line-of-Sight, and for the latter use A*, which works just as well for platformers as it does for top-down maps.

I haven’t made a 2D platform game, but our 3D game uses similar ideas.

Generally, I program the player/AI to use the same “body” class, so they’re both just inserting the same control commands into the same object type. The AI generally spots the player and tries to get close, and will dodge/maneuver in response to player attacks (but doesn’t input read! That’s cheating).

If it gets the job done and it looks and acts the way you want it to, then it’s fine. You don’t have to implement something overly fancy just because it’s “correct,” unless it’s harming performance in some way.

I think for simple 2D platformer games, using raycasting is a bit overkill, instead you can get around with a circle with a defined radius of visibility around the area of the player.

(Excuse my poor drawing skills). Any enemy once got a event with circle of visibility, can attack the player and charge towards him. This is more simple in my opinion.

@SHC that’s also what I do in my game. I have a rectangle that is larger than the enemy, and if the player intersects with that rectangle, enemy is triggered to attack.

You seem to have associating one extra rectangle per each enemy aren’t you? I’d instead associate one large rectangle with the player, and if the enemy intersect the large rect, then only that player will rush and attack.

Though both works, it is just an optimization to remove extra shape instances. Even though you went with either way, no penalty will be observed if you have a broadphase that prunes away unnecessary checks.

Thanks all,

Some good suggestions and comments.

I do use ray casting, reason I do is so player can hide behind blocks in my game. I only draw one line using integer math from the enemy to the player
so it is very quick, if the line hits a block then enemy cannot see the player and thus doesn’t go into attack mode.