Object not moving backwards.

I’m working on a turn-based combat system and my combat system is complete. Now I’m working on implementing the animations. The part I’m stumped on is moving the models back and forth from their starting position to the position where they attack each other. The following code should move the player model up to the enemy and then back away from the enemy. The problem is that it goes up to the enemy model, and then just stays there. It won’t go back. Help?

if(Vector3.Distance(enemy.position, player.position) > 1){
    	player.position += player.forward * moveSpeed * Time.deltaTime;
    	if(Vector3.Distance(enemy.position, player.position) < 1){
    		if(Vector3.Distance(enemy.position, player.position) < 10){
    			player.position += -player.forward * moveSpeed * Time.deltaTime;
    		}
    	}
}

it all went south at like 5, so let’s say the distance is 10, then the player will move to the enemy, untill the distance is lower than 1. then it’ll check if it’s lower than 10. so it’ll only move back when it’s lower than 1.

Always work with states! it gets messy otherwise, or just use the in build animation window, that’s even better!

As for the states, use an Enum and a switch case to detect which case it is in. It’s more readable :slight_smile:

if(Vector3.Distance(enemy.position, player.position) > 1){
player.position += player.forward * moveSpeed * Time.deltaTime;
}
if(Vector3.Distance(enemy.position, player.position) < 1){
if(Vector3.Distance(enemy.position, player.position) < 10){
player.position += -player.forward * moveSpeed * Time.deltaTime;
}
}