Introduction: In this blog post I will start implementing the core mechanics that will make up the player and enemy interactions, this will include the player movement, player wall bouncing, enemy movement and enemy attack. These functions are key to the base mechanics of the game; can the player complete obstacles in the levels and can the enemy detect the player and kill.
The first step was to create the player movement which I used from one of my previous projects in the second year from my zombie survival games. However I did modify this
Script
Player Movement –
![](https://shonenburst.wordpress.com/wp-content/uploads/2023/02/carbon-13.png?w=558)
Player Movement
The player movement is controlled by the default controls for unity, using WASD to move round in the scene and then the mouse to look round using another script attached to the player and main camera (“Look X” + “Look Y”). Using the unity input i got the axis of “horizontal” and “vertical” to control the camera (this was taken from my zombie shooter game). However I changed the original script to use direction to update the player when moving forward through length measurement using magnitude, it takes the coordinates of the x and y from the players input and then calculates the target angle from the way they would be moving. Takes the rotation of the quaternion and converts the target angle through a eluer to convert the angle when the player moves.
A have a few issues with it, when the player moves forward there is a slight drift to the right or left but I am not too sure how to fix it, however it does work it is just not as slick as I would like. This can be a later fix when I have completed every thing else, its just a minor perfection I would like to make.
Jump
Then implemented a jump function which checks the player is grounded using an if statement and then applies jump speed, the jump is equal to gravity multiplied by time dot delta time. The else statement simply is just the walking function.
I noticed that the movement of the player is not very fluid and has some input issue where the player moves diagonal when the player presses forward. This is an issue that needs to be fixed so the core mechanics work.
Wall bounce
The wall bounce mechanic from the tutorial that I was following was suppose to send the player in the opposite direction, it works but does not work as intended. This is not a massive issue but I am going to try resolve this by following the tutorial earlier to try fix it. However the raycast is being recognised when the player touches the wall to bounce of it. However the player can then just keep bouncing off the same wall to scale an obstacle which does affect the effectiveness of the mechanic. I am going to follow the tutorial again and see where my code differs in an attempt to get it to work as intended, its not a major issue but I feel like this affects the gameplay.
New Player Movement (script)-
![](https://shonenburst.wordpress.com/wp-content/uploads/2023/02/carbon-14.png?w=696)
So I changed what I did with the getAxis to GetAxisRaw because smoothing is applied to getAxis. This smooths the direction over time where as the GetAxisRaw does not apply the smoothing and inputs it automatically without smoothing over time (time.deltatime) by directly using the magnitude that has been applied in the script. This seemed to fix some of the issues that I was having with the movement going diagonal slightly after inputting a direction.
Instead of trying to use direction and prevDirection to get the last move equal to the current move I followed his script and made a vertical velocity that uses the jump force instead of jump speed to multiply the vertical velocity for the wall bounce to happen when the raycast detects the wall. Trying to kit bash one of my older movement scripts to the tutorial was what was causing the function to not perform correctly so after using his way of using move Vector and breaking down the jump force, gravity, vertical velocity, speed and last move made it much simpler to get the function work. So it was mainly two lines of code which was placed wrongly and was making the wall bounce not register properly, now that it has been reformed the code seems to work properly for the intended purpose. Also the raycast now is only being detected once now when the player wall bounces instead of multiple raycasts hitting the wall like before.
Reformed Movement and Wall bounce:
Cinemachine added instead
reasons:
- Reduces the clipping from the camera from going outside the walls
- Ease of use and simplicity
- Smooth a fluid rotation of the camera
- Very customisable
![](https://shonenburst.wordpress.com/wp-content/uploads/2023/04/image.png?w=318)
Focus on the next blog post:
– Setting up two types of enemies that will be in the game (1 ground enemy and 1 flying enemy)
Many Thanks,
James Lacey