- Home /
Make player automatically move up on a tile
Hello!
So what I'm trying to do is have it so that when my player touches a tile on the side, he automatically moves up on top of it. Kinda like how in Starbound and Terraria when you move next to tiles of a certain height, you automatically go up it.
So far I've made code that checks if the player is touching a wall left and right, and then I have some movement to go up it.
The only problem is if I have a big wall of blocks, the player will still move up the big wall of blocks. I want it so that it will only move up the 1x1 block if there is no block above it.
Here's a snippet of my code so far:
if (canMove) {
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) {
if (groundedD) {
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
}
moveVelocity = 0f;
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
//Debug.Log("left arrow");
if (touchingWallL) {
gameObject.transform.position = new Vector3(gameObject.transform.position.x - 1f, gameObject.transform.position.y + 1f, gameObject.transform.position.z);
rb.velocity = new Vector2(0, 0);
moveVelocity = 0f;
} else {
moveVelocity = -moveSpeed;
}
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
//Debug.Log("right arrow");
if (touchingWallR) {
gameObject.transform.position = new Vector3(gameObject.transform.position.x + 1f, gameObject.transform.position.y + 1f, gameObject.transform.position.z);
rb.velocity = new Vector2(0, 0);
moveVelocity = 0f;
//rb.velocity = new Vector2(rb.velocity.x, jumpHeight / 1.8f);
//moveVelocity = 2f;
} else {
moveVelocity = moveSpeed;
}
}
rb.velocity = new Vector2(moveVelocity, rb.velocity.y);
}
Answer by lunoland · Aug 08, 2016 at 10:55 PM
It's hard to say for sure without knowing more about how your game is set up, but it sounds like you could just add a check to only move the player if the block that they touch does not have another block on top of it.
You could detect this by doing a raycast upwards from the block the player is trying to move to.