- Home /
How do I check where my character is moving before he moves there?
Hello, and thanks for reading.
I am starting an isometric game that works kinda like pokemon, moving through a space of blocks, but in 3D. I already programed the basic movement, so that the player is able to move the character in the x and z-axis.
In simple words, the problem is that when the player presses a key to move, I need to check the block where the character is moving before he moves, so that I can check if it has another object over it (through a tag in the block called Collision) and that the altitude difference between this block and the current character position isn't too big so that he can walk onto it. Also, if this conditions are met I need to see how much in the y-axis the character needs to move so that he doesn't go through the floor or flies.
here's the function (C#) of the movement:
void MovementManagement ()
{
string dir = "n"; /*direction in which the character is moving: u: up
d: down
r: right
l: left
and combinations.
*/
if (canMove && !isMoving) //Checks if the character isn't moving
{
//When the player presses a key, we will add a direction to the position
//using Vector3."anydirection" will add 1 to that direction.
//also we'll check if we are at the new position, so that the player can
//only move if he is already in a tile.
if (Input.GetKey (KeyCode.W) && tr.position == pos)
{
dir = "u";
pos += 2 * Vector3.right;
}
else if (Input.GetKey (KeyCode.S) && tr.position == pos)
{
dir = "d";
pos += 2 * Vector3.left;
}
else if (Input.GetKey (KeyCode.D) && tr.position == pos)
{
dir = "r";
pos += 2 * Vector3.back;
}
else if (Input.GetKey (KeyCode.A) && tr.position == pos)
{
dir = "l";
pos += 2 * Vector3.forward;
}
//if (!the block where the character is moving has the tag 'Collision')
//{
//if (dy < 0.5f) walk0
//else if (dy < 1f) walk1
//else if (dy < 1.5f) climb
//else don't allow movement
isMoving = true; //tells the other scripts that the character is moving.
transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);
isMoving = false; //tells the other scripts that the character stopped moving.
//}
}
}
And here's an image of how the demo map works, to illustrate the discrete "movement by blocks", in case I wasn't clear enough.
Thanks in advance :)
Answer by barbe63 · Jun 22, 2015 at 04:53 AM
How I would deal with your project is making an array of array of floats to represent your map. Floats representing the altitude of your walkable tiles or for example -1 if it's unwalkable. Then I would make a level editor to set both of your level and the array by using a text file. You will then just have to make the difference between the tile of your position and the next to allow or offset the animation. Lots of job but as a level editor is a must have in a tile based game I really recommend this. You would most likely gain time in the end.
As another approach you can also use raycast to see if there is an obstacle in the direction you are going to.If nothing is touched, another one directed to the ground with an offset based on the direction you want to catch the tile you want to move on(and therefore its transform.position.y) and do the difference with the character transform.positon.y.
I don't have enough data to decide which is the best. The second is simpler but I'd probably prefer the first for the gain on the long time(and no need of raycasting).
I was thinking about doing something like the first option you proposed, though I'm just starting to work on Unity and I'm not an expert on the subject... but I guess nothing bad would come from trying, thanks for the idea :)