- Home /
2.5D terrain height raycasting help
Long story short, I'm trying to set up a top-down 2.5D game with multiple levels of terrain that the player can jump between, and I'm at my wit's end.
I have a system set up where the player has a currentGroundLevel that represents the lowest point they can fall to and a currentHeight to represent their height while they are jumping. Each terrain tilemap has an int that stores its level and a script that tells it whether to be a trigger or a collider depending on the player's currentHeight. This bit works great and only allows the player to pass if they're at the right height.
The problem is updating the player's currentGroundLevel when they move to a different piece of terrain. The following script is my latest attempt at this, using a raycast to find the terrain directly beneath the player and get its terrainHeight.
void UpdateTerrainHeight() {
Vector3 start = transform.position;
Vector2 direction = Vector2.down;
RaycastHit2D hit = Physics2D.Raycast(start, direction, 0.1f, terrain);
Debug.DrawRay(start, direction, Color.green, 0.1f);
if(hit) {
Debug.Log("Found something");
if(hit.collider.gameObject.tag == "ground") {
Debug.Log("Found terrain");
currentGroundLevel = hit.collider.gameObject.GetComponent<terrainController>().terrainHeight;
}
}
}
For some reason the Raycast2D always returns empty, and I have no idea why. I feel like I'm missing something.