- Home /
Moving the player toward a direction
I'm working on a dungeon crawler kind of game(Dungeon Master style) and I'm stuck now at making the player going toward a specific direction.
I'll try to explain what I've done until now, instead of using the method on the wiki about the Grid based movements with Horizontal and Vertical axes, I've been using a raycast method to check for the next tile.
The raycasts cast on all fourth direction only when a gui button is pressed, so I'm not using any input type of control. The raycast check the next tile that have another script with just a bool if is empty or not, and if is empty you can advance.
Now I'm stuck on my coroutine that should run the move for the player, it all checks good and all, but I can't menage to make the player move.
I've been reading that I should use Vector3.Lerp, by using and start and ending position for the movement, for the start position there is no prob I set it just on transform.position, but I have no idea how to calculate the ending position.
Thats my coroutine:
public IEnumerator MovePlayer (string dir) {
_startPoint = transform.position;
if(!_isMoving){
_isMoving = true;
if(dir == "forward"){
Debug.Log ("MOVE FORWARD");
}
else if(dir == "backward"){
Debug.Log ("MOVE BACKWARD");
}
else if(dir == "left"){
Debug.Log ("MOVE LEFT");
}
else if(dir == "right"){
Debug.Log ("MOVE RIGHT");
}
while (true) {
yield return null;
}
_isMoving = false;
}
}
Is nothing fancy, but I don't know how to set up the whole thing to actual move the player, any help would be appreciated.
Answer by stingman · Jul 09, 2012 at 10:33 PM
Since you are using buttons to determine your player path you need to set your end destination as the location your button recognizes as an empty tile space as that is where your player will move. You need to then define 2 transforms: 1 for that tile and one for your current position. Here is an example... this is untested but it will definately get you in the right direction:
Transform startPosition;
Transform tilePosition;
float moveTime = 1.0 // This will be the time variable that will determine how long it will take your player to move between the 2 transforms
//whatever function you will call to make your character move when clicking a button.
void OnGUI(){
transform.position = Vector3.Lerp(startPosition, tilePosition, moveTime * Time.time);
}