Friday, July 22, 2022

First Person Movement and Double jump

 


How I set up my first person movement and a double jump

Scene Set up


Firstly I set up my player character with two empty child game objects one named orientation and the other camPos. 



I also set the player's rigidbody to interpolate and collision to continuous.








Camera Script

Next, I created a script called playerCam to control where the camera is looking as well as its position.
I made a float for the mouse sensitivity for both the X and Y axis and a transform for the orientation and the camPos as well as a float for the xRotation and the yRotation.


Then I set it up so that the cursor is invisible and keep it stuck in the middle of the screen. In the Update function, I set the transform of the camera to the camPos position. Next, I set up floats in the update function to get the mouse inputs in real-time multiplied by the sensitivity and made the X mouse input add to the yRotation and the Y mouse input subtract from the xRotation also I used Mathf.Clamp to keep the rotation within 90 degrees so the camera cannot look too far up or down. Then I made both the camera and the orientation rotate with how you move the mouse.

Movement Script
After I finished with the camera script I set up my movement script with a move speed float as well as a float to set how much drag the ground has so the character does not slide while on the ground. Then I needed to check to see if the player is on the ground so I made a raycast I used a float for the player's height a layer mask to check for the ground layer and a bool. Then when I made the actual raycast I got the player position downward and added the height and looked for the ground layer. As well as a transform for the orientation floats for the horizontal and vertical inputs and a Vector3 for the movement direction. Then I got the rigidbody and froze rotation so that the character does not fall over.

I set the horizontal and vertical input based on the keyboard input and called it in the update function. In fixed update, I called the move function so that it only plays with physics and not every frame. In the move function, I calculated the direction of the player based on the orientation forward direction multiplied by the vertical input added with the orientation right with the horizontal input. Then I added force to the rigid body based on the direction I just calculated normalized multiplied by moveSpeed and 10 to make it faster without having to have a huge move speed number and used forcemode.force since it is going to continuously add force to move the character.


After that, the character would accelerate faster than what I wanted the max move speed to be so I had to cap it. So the speed control function is called in the update function and caps the velocity so it will not be faster than the maximum velocity. So first I calculated the velocity of the rigidbody then I compared that value to the movement speed value and if the velocity is higher than the move speed then it limits the velocity so to do that I calculated the max velocity by normalizing the current velocity and multiplying it by move speed then I set the rigidbody's velocity to that for both the x and z axis.

Jump Script 
Next is the jump portion of the script first I made a float for the jump force and air multiplier which will adjust how much movement the character can have while in the air then I made a jump number int and a jump reset int.
After that in my update function under if on the ground set the jump number to the jump reset. Then in my input function, I coded in when I press down the spacebar and if I'm on the ground it calls the jump function however if I am not on the ground and if my jump number is greater than 0 it will reduce the number by 1 and then call the jump function.

In my jump function, I set it so that the y velocity is reset to keep the jump height consistent then I add force upward to the rigidbody multiplied by the jump force in impulse mode since it is just one sudden burst of force and not continuous like with movement.


Then in the move function, I added an if statement, where it checks if the character is on the ground or not, if they are on the ground movement is normal if in the air however it gets multiplied by airMult which lowers the move speed making the character slower while in the air.


Inspector Script Settings




















No comments:

Post a Comment

My First Podcast

Nat20Coding · My Game Programming Thought Process I talk about my thought process behind making my game programs