Hi Guys, I have a problem I cant find a solution for.
I have various units on the field (that is divided into tiles, and it’s a turn based game) and need them to stop moving when stepping on a tile that is next to an enemy unit - so a classic turn based system if you will.
Everything is working fine but I have no idea how to implement that stop mechanism. I do check if a tile is clear like this:
I suppose it would be possible to cast a circle also that is big enough to check all near tiles, and then stop the movement but that is growing over my head a bit. The movement is as follows:
Any suggestion would be much appreciated. Again, I “just” need to check somehow if a unit is on a tile that is next to the tile I want to move and stop the movement part. Argh.
I might be blind, but I don’t see where you’re calling IsClear at in your movement script. But either way, before you execute a move or after you execute a move, you will want to check if it’s clear, and if it returns false, then you have an enemy and do whatever, if it is clear, then you continue to move.
In your scripts case, you’ve got two while loops which each need to check. My suggestion is before the first while loop you create a bool. Something like “foundEnemy = false” and then in the first while loop, you are checking for the enemy and you’ll want the while condition to loop while foundEnemy = false. Second while loop needs the same condition. If you find an enemy, you set foundEnemy to true which breaks out of the while loop. Note if you want the second while to execute even if you find an enemy if the first, then use “break” instead to exit the loop early.
Really in the end, it depends on your planned behavior.
Hi Brathnann, thanks for the reply! You are not blind I dont call it at the moment as I was/am not sure what do do. Your suggestion sounds great, I will look into this. However one question, what would be the best way to check if an enemy unit is nearby? the planned behaviour is pretty easy: enemy stands at tile a, so if you want to pass the tiles above, under, left or right from the tile the enemy stands on, movement instantly ends.
If this is a grid, then it should be easy to check the neighboring squares if that is what you want. So if you move into a square, you can check the neighbors to see if they are occupied and what they are occupied by to determine if you can move again. At least, that’s how I would probably set it up for a grid based check personally.
Are you tracking the positions of everything somewhere? You may not need to (just use proximity detection), but if it’s a grid-based game, you probably want to track things with a 2D array (or more than one, depending on what you’re tracking). Then it’s quite easy to check adjacent tiles, you just have to account for the edges.
The positions are not tracked anywhere, no. I thought it would be possible to use an overlap circle to check if an object with a specific tag is in range X, would that work or would it be work for nothing?
No, that would definitely work, and would be easier than tracking in a big array. The only reason I mentioned the array is that often it’s helpful/needed to track everything continuously for various game logic. If all you need is proximity detect, the overlap circle should work great.