RTS - targeting, moving close to target

Started by
2 comments, last by BeerNutts 9 years, 6 months ago

Hi

Im doing a tilebased (A* pathfind) rts (think warcraft 2) with large tiles (one unit per tile). When i issue an attack order on a specific enemy i want to get close to it and then attack.

When i click an enemy i find a free, walkable adjacent tile (preferably the tile closest to where the attacker is, so the unit doesnt walk all the way around the target and hit it from behind) and set the attacker to move there. When close enough for attack (melee or ranged attack) i stop moving and attack the target.

But the target may move at any time, so i need to recalc the "move to this tile to be able to attack" and redo pathfinding. This may get slow right? And ranged units dont need to find an adjacent tile, just any tile close enough to the target to be able to attack it.

Any good way of doing such things? My method seems crude to me. What if there are many units involved? (selecting 30 of your guys to attack someone).

Thanks

Erik

Advertisement

What I've done is, if you select an enemy unit to attack, the player's unit marks that enemy, and simply moves towards the tile the enemy is on. Part of the logic in the player's unit should be CanAttack(), meaning, is it close enough to attack. So, while you're target tile is the one the enemy is standing on, your unit will stop once it's in attack range (a few tiles for ranged units, adjacent tile for melee unit).

If your enemy target moves, yes, you have to recalculate, but you should always use the CanAttack() to over-ride whether your unit has reached its intended tile or not to stop moving and start attacking.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

ok but the tile the target is currently standing on is not walkable (and not "pathfindable") though since its blocked by the target unit itself. Maybe make an exception for that specific tile...

But basically this is how its done?

ok but the tile the target is currently standing on is not walkable (and not "pathfindable") though since its blocked by the target unit itself. Maybe make an exception for that specific tile...

But basically this is how its done?

I think that's flawed. You should be able to select any walkable tile for pathfinding, whether a unit (enemy or friendly unit) is on it or not.

If you select a tile a friendly unit is on, the tile is the destination; if the friendly unit doesn't move, then your unit will stop when it gets as close as it can to that tile. If the friendly unit does move, then you will walk onto the tile as if the friendly unit was never there.

If you select a tile an enemy unit is on, then the enemy unit is the destination, and your pathfinding may have to be updated if that unit moves.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement