- Home /
Realtime Turn Based game, random movement?
I have a little problem with a random movement function in a (what I call) realtime turn-based game (the player moves in realtime, but every action is considered a turn and the enemies will move at the same time. If the player doesn't move, nothing else will). All on a Hex-Grid.
I have a script attached to the player that will set a turnActive variable 'true' for a set amount of time and this variable is used to determine if other objects can move or not. It does work up to here, but I now wanted to have some random movement for the enemies that are not aware of the player, but I want them to not change direction during one turn, only between two turns. I thought I could use the counter from the player's script to create a random number at each turn, but it doesn't work, no matter if I use '== 0' or '== turnDuration' in my if Statement. It does work if I use '== turnDuration -1', but then I have erratic movement for the last 1/15 of a turn.
statics.js:
static var playerActive = false;
static var turnDuration = 15;
static var turnActive = false;
static var currentDuration = 0;
Player.js:
function Update () {
if(Input.anyKey) {
statics.playerActive = true;
}
if(statics.playerActive) {
statics.turnActive = true;
statics.playerActive = false;
}
}
function FixedUpdate () {
if(statics.turnActive) {
statics.currentDuration += 1;
if(statics.currentDuration == statics.turnDuration) {
statics.turnActive = false;
statics.currentDuration = 0;
}
}
}
BasicEnemyBehavior.js:
var speed = 1; //number of tiles per turn
private var heading;
private var ranNmbr = 0;
private var r = 1;
private var g = 0.5;
private var b = Mathf.Sqrt(3)/2;
function FixedUpdate () {
if(statics.turnActive) {
ranMove();
}
}
function ranMove() {
if(statics.currentDuration == 0) {
ranNmbr = Random.Range(0, 6);
}
switch(ranNmbr) {
case(0): heading = Vector3(r, 0, 0);
break;
case(1): heading = Vector3(g, 0, b);
break;
case(2): heading = Vector3(g, 0, -b);
break;
case(3): heading = Vector3(-r, 0, 0);
break;
case(4): heading = Vector3(-g, 0, b);
break;
case(5): heading = Vector3(-g, 0, -b);
break;
}
transform.Translate(heading * speed);
}
Edit: I pulled the random number generator out of ranMove and into FixedUpdate (before the if Statement) and now it seems to work. But I now have a new Porblem: I created a Prefab from my test enemy and added additional copies to the scene, but they move a little bit faster than the original and thus stray off the grid. I don't know if that is connected to the scripts, but I haven't changed anything else...I guess I have to create an absolute position for the movement instead of a relative one...