In the second session of the first week we were told to add more mechanics to our character controller, so I came up with the ideas of sprinting, jumping, crouching and a mouse follow system. To sprint I simply created a second variable with a greater value than the initial speed, then set that as the speed variable to be used while the shift button is pressed. In order to jump I needed to obtain the objects Rigidbody and apply a force upwards when the appropriate key is pressed. For the crouching mechanic I set the scale on the Y-axis to half upon pressing a button once and to double back upon pressing it again. I also reduced the speed and took away the sprint ability while crouched for realism.
 |
Crouching mechanic code |
In terms of the mouse follow, I struggled to make a script for it myself. So I looked on the internet and found a script that works perfectly, then spent time understanding how it works and editing it to make it more unique to me. It involves creating 2 enumerations: MouseX and MouseY, which will control the relevant axis. The script is attached to both the character and the camera with the MouseX setting chosen for the character and MouseY being applied to the camera.
 |
First person mouse follow script |
MouseX allows for 360 degree rotation in the X-axis and therefore allows the character to rotate in a full circle. Mouse Y is limited to looking 60 degrees both up and down, creating more realism and stopping strange viewing angles. The reason this setting is applied to the camera is applying it to the character would cause them to rotate in the Y-axis and ruin the other rotations and character movement. Therefore the camera and character rotate independently on different axis but as the camera is a child of the character, it rotates with it in the X-axis.
 |
Unity scene in game showing angled look at object |
No comments:
Post a Comment