For my Platformer prototype, I set out to include elements that I have never tried before such as creating an enemy AI.
I used vertex snapping to create my environment quickly and efficiently. I got the majority of my sprites from kenney.nl, but I made my own enemy sprite.
In the Start function of the PlayerBehaviour script, it is assigning the RigidBody2D and SpriteRenderer of the game object that this script is attached to, in this case – the player.
The if statements for moveInput control when the sprite flips according to the player’s input. The overlap circle function checks if it is colliding with the ground.
A problem I had with Unity is that in my original code, the Update function was actually a FixedUpdate function. This would normally be correct as the jumping is physics related, however, when the function was a FixedUpdate function, it only jumped when the spacebar was pressed multiple times and it felt very unresponsive. Changing it to an Update function fixed the issue.
In the Obstacle script, I used the built in Unity function OnCollisionEnter2D to control how the player’s collider reacts when in contact with another. For the spikes and enemy, I created a Vector3 for the respawn position back at the start of the level. A Vector3 controls the position of a game object. The player prefab is then instantiated and the original disappears.
For the key, if the player reaches it, the win function is called which calls the Setup function in the Win script.
For the win screen, I adapted the game over screen that was used in both the Space Invaders and Top Down Shooter games.
I wanted to add another element to my game, and so I decided to add a ladder that the player could use to access higher platforms in the level.
If the player is touching the ladder and the absolute value of the vertical input is greater than zero, the climbing physics are activated.
One major issue I had with the ladder was my player not being able to pass through it. After minutes of looking at my code trying to find an issue, I realised I had forgotten to tick the “Is Trigger” box. I think this is a problem with Unity in general, the fact that most of the time the problems are so miniscule and yet they stop your game from functioning properly and sometimes the problem isn’t even your code.
The last element I wanted to include was an enemy. I didn’t want the enemy to just stand still however, I wanted him to patrol from side to side of the platform he was on.
To do this, I set mustPatrol equal to true at the start of the game which, in turn, calls the Patrol function. This function controls the movement of the enemy AI through its RigidBody2D and more specifically, the velocity. It also calls the Flip function if mustTurn (which is controlled by OverlapCircle much like the player) is true.
Multiplying the walk speed by -1 when the sprite is flipped means it continues to walk forwards.
If I were to improve the enemy, I would make it so when the player jumps on his head he dies, but if the player touches anywhere else, they die and respawn back at the start. To do this I assume you would have two seperate colliders on the enemy.
To improve my platformer further, I think a timer could be implemented. For example, you would only have 15 seconds to complete the level and if not, then you restart back at the beginning.
Additional Research :
Unity 2D Fan Club (2013) Unity 2D Tips: Vertex Snapping, Level Building and YOU! [Video]. Available online: https://www.youtube.com/watch?v=O9X6y8WLo0Y [Accessed 6/11/2021].
Bendux (2021) How To Make 2D Ladders In Unity [Video]. Available online: https://www.youtube.com/watch?v=yyg0yV2roPk [Accessed 8/11/2021].
Mike Scriven (2020) 2D enemy PATROL AI in Unity – Part 1 [Video]. Available online: https://www.youtube.com/watch?v=rn3tCuGM688 [Accessed 10/11/2021].
LINK TO MY GAME : https://ethanconyers.itch.io/platformer