Friday, July 29, 2022

Sprint and hover system with 3rd person camera swap


Quick video to show how it looks in practice


Code and description for how I set up a sprint system a hover system a stamina/energy bar for both of them and a 3rd person camera and how to swap between 1st person and 3rd person camera


This post uses code from another post if interested here's the link.
https://nat20coding.blogspot.com/2022/07/first-person-movement-and-double-jump.html#more
Stamina/Sprint

First I set up my stamina since I am using a slider for the stamina bar I needed to use UnityEngine.UI. I also added a walk speed and sprint speed float. Then I added a float for the current stamina, max stamina how much it costs to run and how much stamina the character gets back per second when not running.


Then I set up an enum to keep track of the character's movement state. Also in the start function, I set the stamina slider's value to the max stamina and the current stamina to the max so the character starts with max stamina.


In the update function, I state handler and the stamina bar functions. State handler sets the move speed based on the input so if I am sprinting it sets it to the sprint speed and if I am on the ground and not sprinting it sets it to the walk speed.

The stamina bar function drains my stamina over time while sprinting and regens it while not sprinting by checking the enum I made earlier. Then it sets the stamina slider's value to what the current stamina is.


Hover

For the hover part of it, I put a float for the hover time left the cost of using it per second how much the character regens per second how much upward force and the move speed while hovering as well as a slider for keeping track of how much time till it falls.


After that, I added a hover to my move states and in the start function, I set the slider's value to the maximum hover time and the leftover hover time equal to the max hover time. and in the update function, I called the hover bar function.


In the hover bar function if the character is hovering I decrease hover time by hover cost per second and if not hovering and if on the ground increase hover time by hover regen per second. As well as set the slider's value to the hover time.


In my input function, I removed my double jump and replaced it with while spacebar is held down and hover time is still left it propels the character upward using forcemode.force since it applies force continuously



Then in the move function, I added an else statement to check if the character is hovering and if so the speed at which they can move is set to hover speed instead of slowing it with airMult.


Third Person Camera

First I created an empty game object to hold my cameras and I made a second camera. Then using the cinemachine addon I created a free look.



Then I created a script to control my third-person camera. So I needed a transform for the player and the orientation and needed to grab the rigid body of the player. In the start function, I made the cursor invisible. In the update function, I calculated the direction that the player is looking by getting the camera's x and z transform and then said that the orientation forward direction is the view direction normalized to make it a whole number. Then I made a float for the horizontal and vertical input and calculated the input direction. Then if the input direction is not zero the player turns to face that direction.



Then I added a Cinemachine collider to the freelook object to make the camera pass through walls if they get between the camera and the player




Camera Switch

After that, I set up a script so I could switch between the two cameras at will. So on the cameraHold game object, I removed the part of the script from my first-person camera that moved the camera to the camera position game object and put it on this script so that both the cameras will move with it. Then I made a bool to check if the first-person camera is true or not then set it true and made the first camera active and the second camera inactive. Then I call the camera switch and if I click the p key based on if the bool is active or not it switches which camera is active.


Freelook object 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