Game Dev in Progress… Day 27
Hello and Happy Thanksgiving!!! Today I wanted to work a bit more instead of taking the holidays off (I don’t really celebrate Thanksgiving…) I made some progress in starting out the 2.5D course and got 36% of it done today which I think is pretty good progress. I’ll go over some of the things that I learned and some of the problems I came across. Then I will show you how I solved them.
The first feature I had to implement is how the player is going to move around the 2D environment. The game that were suppose to implement is a 2D platformer type of game and that means moving from left to right and up and down. We used a powerful component called a Character Controller which has some neat properties and methods that had to do with moving a character. One of the methods we used was the Move method which we can pass a Vector3 that essentially tells it to move to that direction.
We also learned how to implement a jump mechanic. In order to emulate gravity, in our script we had a float value called gravity that is constantly being subtracted from the y position of our player every frame. So in order to create a jump we simply had to add another float value to our y position when we hit the space key. The problem is that because this is being updated frame by frame, gravity is fast and immediately grounds us before we can see an actual jump. To remedy this, I found a solution in the API for Character Controller Move method. They multiplied gravity by Time.deltaTime and Mathf.Sqrt(_jumpHeight * -3.0f * -_gravity) to calculate how high to jump. This way gravity won’t pull the player to fast and will let it hang in the air for a bit of time.
Another problem is implementing a double jump. My double jump would make me jump infinite amount of times. I quickly remedied this by having a bool value that checked if I can double jump when I’m not grounded. The double jump also had a very high hang time and I would float for too long. I figured this problem out by setting gravity a bit higher so it’ll pull the player much quicker.
The second feature was to implement the Coin collectables. These coins would be collected and will update a UI text that shows the number of coins collected. The feature was simple to implement but I did ran into a bug where some of the coins would add 2 rather then 1 for the coin count. This took me awhile to figure out but when I found out made me really stupid. What I didn’t know is that for some of the Coin objects I added the Coin Script 2 times. So I had to 2 of the same script attached to those objects which would call the addCoin method 2 times…. yeah it was really stupid. I removed the duplicate scripts and everything was working again.
The third feature to implement was a moving platform. Everything was going smoothly until I had to make my player move with the platform. The problem I had involved the player not moving with the platform and falling off once the platform moved out under it. My code would set the player object as a child of the moving platform so that way it moved along with it. What I didn’t catch was that I was doing player.tranform.parent = transform.parent. In that code, I was setting the parent of the player as the parent of the platform instead of the platform itself. It was such a simple mistake…. Another problem I ran into was having the player jitter around. The remedy was using FixedUpdate rather then Update method.
The last section I was able to finish was the lives. When the players falls off they should respawn and have there lives subtract. When the lives reach 0, reset the entire level. Seemed easy at first but ran into a problem where my lives was being subtracted by 2 rather then 1. At first, I thought I made the same error as I did for the coins but it wasn’t that. After messing around with it, I realized that my player was maybe moving to fast and thus it was registering twice? So I redid my code a bit and instead used a DeadZone area which using an OnTriggerEnter, would subtract my lives. This worked but now my respawn didn’t work. Instead of trying the figure out a new solution with my DeadZone, I already had my respawn working on my first try and used that. My first code would check if the player’s y position was less then a certain y (in my case it was -5). If it is, respawn the player. And this worked.
Here is a short video of what I have:
Sorry for the long winded rant about my troubles for the day. I learned a lot of things such as I am now going to constantly check if I added a script twice in an object… Also some of the hardest bugs can have the most simplest solutions. Anyways, have a wonderful Thanksgiving!!!
_
Cristian Aspacio