
Adventures of Ben
I have to create a 2D platform game that includes; A win state, a lose state, a patrolling enemy, a collectable item, a jeopardy mechanic, a ludic tutorial, an animated character, a start menu and a game over scene.
To do the win and lose states i will use canvases that will be activated on collision with a certain tag.
For the collectable item i will put the sprite image of the item on a canvas and activate the canvas also on collision.
For the patrolling enemy i will use a raycast to make sure the enemy is still on the platform and have him flip when the platform ends.
For the ludic tutorial i will use a canvas put change the option to world space to add the tutorial and keep it where it is instead of following the character.
For the animation i will use the animator controller and the sprites that came in the sprite package.
As for the start menu i will create a new scene and use the application load level function to make it go to the main game play scene.

For the patrolling enemy i set the speed of the enemy to go at a constant rate no matter what using the transform vector 2 code. To make him patrol back and forth i used the raycast to measure down a distance and make sure there was a box collider underneath of him and if there was nothing detected by the raycast it would activate the flip which is controlled by saying that if the enemy is moving right and detects nothing then it transforms the sprite and flips it by -180 on the y axis. And then it says if its not moving right and doesn't detect the collider then then change the sprite back to normal.
I have set the health so that if the health equals 0 it will trigger an animation and will start the function labelled "die". The function "die" says that after a certain amount of time then destroy the enemy and i have done it after time because it lets the animation have time to play through.
I also added an extra bit that stuns the enemy when damaged for a brief period of time however i have set the enemies health to 1 so that one attack instantly kills them so i don't really need it but left it in in case i change my mind.
For the player to attack i put an if function so that if the attack key is pressed, which in my case is the up arrow key, it triggers the attack animation and looks for enemies in the 2D collider. If there is an enemy in the 2D colider when the key is pressed then it will get the patrol script and reference the damage taken part to deal damage to the enemy.
To stop them from spam attacking i put a delay between each attack and set it to 0.3 seconds making it harder for the player to kill the enemy.
I also added a red circle to the attack area so that i could see where i was placing the area in the scene but it wouldn't appear in game by using the on draw gizmos selected function.






The timer works by setting the starting time using a variable function that basically says that it should -1 each time from the original number each second and make the new number appear instead of the original and this happens each second.
I also added the game over canvas and restart button setting them to false in the start until the timer runs out and then they are set to true which activates them.
I did the same for the player but set him to true at the beginning to make sure he works and then false when the timer ends to end the game and stop the player from continuing to move.
Furthermore i put an extra if statement that says after the timer turns 21 (so at 20) turn the timer red) i did this to put more pressure on the player and make them panic more.
The camera follow code searches for the game object with the tag "Player" and then transforms at the same rate as the character giving the effect that the camera is following them.
The start screen script is done by loading the scene again so the character starts again at the start and all the enemies re-spawn. This is connected to the restart button on a canvas so that you can press it on the start screen.
I then added an if function into the update so that when you press the return key it also loads the level again.
The same principle applies to the restart in game script.
For the collectable i put a box collider on the collectable and the set its tag. Then i put an if statement that states if the collectable collides with a box collider tagged "Player" then it will destroy the collectable.
In the start function of the player script i have set the game over text, the restart button, the win text, the win restart button and the collectable to false so that they don't appear until activated. I then have 3 on collision pieces of code at the bottom with different tags such as "Enemy", "Win" and "Collectable" so that they activate when they collide with the appropriate tags.
In the update function i have a check for movement and a check jump which both look to see if a key is pressed on each frame so that it can do it over and over. The check for movement function says that if the "D" key is pressed then make velocity the same as walking speed. And if the "A" key is pressed then set the velocity the same as negative walking speed. However it also states that if no key is pressed set the velocity to 0.
To make sure the animation is facing the right way i had to create a flip function that inverts the sprite by -1 on the flip scale x and makes the original named is facing right. This makes it so if the player walks right the code looks to see if it is already facing right and if not it will flip the character and if the player moves the character left then the code will see if the sprite is facing right and if it is then it will flip the character.