Game Dev in Progress… Day 28
Hello and Happy Black Friday. Today I worked on one section all day which was the Puzzle Section. In this section, we were given a premade Asset Scene where we can create and test our platformer functionalities. The three main functionalities we had to implement was the elevator, wall jumping and moving a box onto a pressure plate. The hardest of the three to implement was the wall jumping but I’ll start by explaining what I did for the elevator.
For the elevator, there is a button/area where we can press the E key which will call the elevator and move it down. Then when we are on the elevator, we can press the E key again to make it go up. It was a simple implementation where I just had a box collider which checked if the player was in it using OnTriggerStay method. When the player hits the E key inside the collider, it will move the elevator down and switch the color of the elevator switch to blue. If the player hits E again, it will go back up and switch the color back to red. I had problem where sometimes pressing E key wouldn’t work. After looking up this problem, I found a forum post stating the Input.GetKey wasn’t being called every frame. So if I don’t press it during a frame, it wouldn’t be called. To solve this, I created an Update method where I checked for the input every frame. Here is a demonstration of the elevator working:
The wall jump feature took me awhile to figure out. This feature would allow to jump from wall to wall in order to get up the level. In order to this, we used the character controller component that is attached to the player object. This controller has a method called OnControllerColliderHit(ControllerCollider hit) which passes a parameter a ControllerCollider. This ControllerCollider holds information about the object that the controller is hitting. What’s neat about this is that the ControllerCollider has a property called normal which is a normal surface perpendicular to the object hit. We use this property to create a push effect when we collide on the wall by changing our vector to the normal. We also check if we are grounded or not to make sure we don’t get pushed when we approach the wall. The problem I had wasn’t the push but how to go up the wall. I realized it was only pushing me horizontally and wasn’t affecting the player vertically. To remedy this, I just added the jump height while I was being pushed off the wall. Here is the wall jump in action:
The last feature is to move a box over a pressure plate and activate. When it activates, the box will freeze and the pressure plate color turns blue. This one wasn’t to hard as the API for the OnControllerColliderHit already had a working code example that we can base our own code on. I first checked if I was colliding the box using its tag to compare it. If it is the box, I grabbed it’s RigidBody component to check if it’s movable. Then calculated a Vector3 push direction by using the hit.moveDirection.x and 0 on the y and z axis since we only care about horizontal movement. Then we set the RigidBody.velocity of the object = the push direction * the push force (how hard/fast we want to push it). Then we simply created a script the checked if the box was on top of the pressure plate by calculating the distance of the objects center. If the distance was less than 0.5 than freeze the box by setting .isKinematic property of its RigidBody to true. We also change the color of the pressure plate to blue at the same time. Here is my implementation in action:
Overall, the elevator was a bit hard but I managed to figure out the bugs and fix it. Most of the bugs of the elevator were pretty minor and was able to fix relatively easily but the E key press was what took me to longest. The hardest feature was the wall jump. It took me awhile to figure out that I wasn’t adjusting the height of the jump. Because the bounce of the wall was very subtle, I couldn’t visually see that it was only applying force on the x-axis and not also the y. Also the double jump was messing me up as well as I thought it was applying the y-axis force but it was just me double jumping. The movable box was pretty easy and didn’t give as a hard time as the other two did. Anyways, have a wonderful weekend!
_
Cristian Aspacio