Taking work which I held on to from my first year, I created a basic movement script for the player. Separately I created updates for moving the player left and right by changing the co-ordinates of their position, and them being positive for the right and negative for the left. For these changes to happen, in the update part of the script, I used the Input.GetKey function so when a named key is pressed, it will move the player.
To make this piece of code simpler I am now using the function Input.GetAxis function which has built controller mapping for unity, them being A/D and Left/Right. So, using a single line of code using the horizontal axis the player now moves more smoothly which will be more beneficial when adding the animations to the character. Also, the function also can be used for analogue stick control if we choose to change to an Xbox/Playstation controller for the final demo.
With the original jump script having a few bugs to it, I decided to change the functions used to make the player jump. It originally used the transform.Translate function to move the character to a new co-ordinate along the vertical axis, then the player will fall due to having a rigid body, simulating a jump action. However, this method for creating the jump makes the movement look too quick compared to how a jump would normally look in game. So, I changed the function so it adds a force to the character rather than moving it to a new location, creating a better and more natural jump. I was also able to fix the bug of the character jumping again in mid-air.
For this I used true or false statements and collisions. The function ‘canJump’ determines whether the character is able to jump into the air, so for the update where the character is projected into the air, ‘canJump’ is set to false meaning they are unable to jump again until grounded. I then ran into another bug in that upon playing the character could only jump once, so again using the ‘canJump’ function, if the player collides with the game floor (which is tagged as ‘Floor’ in Unity0, the statement is set to true so the player is able to jump again.