- Home /
How do I move objects in one executed turn?
I'm trying to make a turn based strategy game but not the kind where players take turns making their moves, but rather, everyone plots their pieces movements while the game is paused then all commands are executed for all players simultaneously.
My question is how can i give a move command to each of my units individually that won't execute immediately but rather when i click some sort of "take turn" button?
additionally the script should pause the game after all the pieces have moved to allow for the next turn to begin if possible.
I know it's really multiple questions in one but I appreciate all responses even if you only know how to partially answer my question.
Thanks everyone.
Answer by spectre1989 · Aug 11, 2013 at 01:59 PM
Sounds like Frozen Synapse =)
What you need is a script which holds a series of commands, which can then be executed. You could do this with an array, but given that there would likely be lots of adding and deleting of commands, I'd advise using a List. You could then use a coroutine to execute the commands. So you might have something a bit like this:
public class Unit : MonoBehaviour
{
private List<Command> commands;
public float movementSpeed;
public void Awake()
{
this.commands = new List<Command>();
}
// This would be called by whatever script is interpreting user input
public void AddCommand( Command command )
{
this.commands.Add( command );
}
public IEnumerator ExecuteCommands()
{
foreach( Command command in this.commands )
{
this.transform.position = this.transform.position + ( command.position - this.transform.position ).normalized * this.movementSpeed * Time.deltaTime;
yield return null;
}
}
}
Note that in that code snippet, it assumes a command just contains a position for now, and it doesn't even detect when that position has been reached so it'll actually stay stuck on the first Command. But you get the idea!