Character WaitForSeconds/Yield

Hi there,
So I have a character moving around in a scene, and need to make the character move in a grid sort of away. Thus, I am thinking of using yield and waitforseconds. any help would be greatly appreciated. here is our coding right now.

private var max_z_value = 1.9;
private var min_z_value = -1.9;
private var max_x_value = 1.9;
private var min_x_value = -1.9;



function update () {
 
if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position.z < max_z_value) {
    transform.position += Vector3.forward;
}
if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.x > min_z_value) {
    transform.position += Vector3.back;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < max_x_value) {
    transform.position += Vector3.right;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x < min_x_value) {
    transform.position += Vector3.left;

}

1 Answer

1

you mean like move from point to point on a grid/array? (so he moves from A to B but skips some and doesn’t exist really between 2 points)

the simplest is to make a 2 or 3 dimensional array of points and move through the array.

The reason you do this (as opposed to simply transform.position + 1 in whatever direction

is for later.

Later on you can use that array as an array of structs and use it to store information about the terrain.

you can do something like.

if array[playerX,playerY,playerZ].IsWater or IsRoughTerrain or

if your about to move to it

if(playerX + 1,playerY,playerZ).IsImpassable
//don’t allow the move

this allows you to basically store however much info you want about that point of terrain and later to modify it even during the game.

That’s good for adding complexity