Hey guys,
So, I took on the Mega Challenge before watching the rest of the video. I really wanted to work this one out, so it took me several days, but I’m glad I made the “detour” (:
https://community.gamedev.tv/uploads/short-url/8zdtRPS1WY3L7fkCsEY1zdpEw2e.mp4
From the video, it doesn’t look like much, but there’s quite a bit going on under the hood. Mainly because the player sprite is not confined to a single tile space (this collider is actually one tile wide by two tiles high).
Having a taller player presented two distinct challenges.
Challenge #1 - Reaching to top of the ladder
When he reaches the top, I didn’t want the sprite to go aaaaallllll the way to the top like this:
So, I created a second, “Upper Ladder Collider” (shown as the green square, which is 1x1 tile)
This collider is only active while on the ladder. When this collider exits collision with the tiles on ladders tile map, the OnTriggerExit2D
for the ladders tile map fires, and the player will exit the ladder.
Additional, the player is placed on top of the ladder.
But wait! There’s more!
The ladders tile map is actually a trigger, so the player can’t “stand” on top of the ladder. So, there are normal Foreground tiles placed on the Foreground tilemap, behind the ladder.
Also note that since the Foreground currently blocks the player, you can’t “move through” those tiles while on the ladder. So the normal collider (the 1x2 mentioned above) is disabled while on the ladder.
Challenge #2 - Reaching the bottom of the ladder
Climbing down the ladder presented its own problems as well. If you’ve reached the bottom of the ladder, the upper tile collider won’t work because it will allow the player to climb too far down, into the ground tiles.
I created another collider, a “Lower Ladder Collider”. This one is similar to the upper collider, but it’s used when climbing down. When this collider has exited collision with the Ladders tile map, the player will exit the ladder.
So, that’s the gist of it! The code for handling the ladders alone became a bit much, so I made it into it’s own Ladder Climb Utils
script and attached it to the player game object.
Edit: One thing I forgot to mention is that, while the player is on the ladder, the gravity for the player’s rigid body is disabled, so he can stay “suspended in mid-air” while on the ladder. The code looks something like this:
m_rigidbody.gravityScale = 0;
m_rigidbody.velocity = Vector2.zero;
When the player exits the ladder, you can restore the original gravity.
I also ended up using various APIs from the UnityEngine.Tilemaps.Tilemap
class such as GetInstantiatedObject
, which can get a tile object at a specified location.
When working with tile positions in the code, they don’t use the usual Vector2
or Vector3
. Instead, they use Vector3Int
which is like Vector3
, but only uses ints.
Also, when working with code, you may find yourself having to convert to and from game world positions and tile map tile (cell) positions. That’s where methods like Tilemap.WorldToCell
and Tilemap.CellToWorld
come in. The former converts game world coordinates to their corresponding tilemap cell location, and the latter converts in the other direction. For the purposes of climbing ladders, I only needed to use WorldToCell
.
However, I ran into some issues when using world positions as is, so later found this solition ( http://answers.unity.com/answers/1440663/view.html ), but instead used Mathf.FloorToInt to get the result I was after.
public Vector3Int WorldToCell(Vector3 vPosition) {
Vector3Int v3iPosition = new Vector3Int(
Mathf.FloorToInt(vPosition.x),
Mathf.FloorToInt(vPosition.y),
Mathf.FloorToInt(vPosition.z));
return (m_tilemap.WorldToCell(v3iPosition));
}
I’m happy with how it turned out. (: There are still a few kinks to work out here and there, and I want to polish it up a bit more, it’s good for now. I’m sure I’ll get those taken care of as I learn more.
Any questions about any of this, please let me know.
Thanks!
- Ziro out.