- Edited
Hello again guys =) ! How are you doing ?
nb: I'm using m/s for speed and velocity and meters for distances in my script. I'm making a game code and trying to implement a close to realistic sprinting and stamina mechanics. In my sprinting script aka Acceleration & Speed switch I try to implement the Stamina and Realistic sprinting mechanics.
My goal is if the player is in the air aka not is_on_floor , then the acceleration switches to : speed / 5 which should make any movement change really slow in the air which I think is realistic ?
Here is my basic mindset behind the stamina mechanics if sprinting<< -My second goal is to make the sprinting mechanics a little more complex. Basically , I want the Stamina which is by default = 100, to deplete in a timeframe of about 60 seconds IF the player keeps sprinting nonstop. -However when he stops sprinting , the stamina bar should refill from its last value to max 100, in a timeframe of about 90 seconds if the last value is from 0-90 seconds which is about 5400 frames @60fps. I could maybe use delta for it ? -The player should not be able to sprint IF Stamina is depleted and inferior of the rate of which it is consumed per second if sprinting aka IF stamina <= sprint_stamina_depletor then sprinting = false and cannot sprint until stamina >= sprint_stamina_depletor
Here is my mindset behind the stamina mechanics if jumping<< -The player cannot jump more than 3 times consecutively in a very short time so I set the value of jumping_stamina_depletor = 100 / 3 and IF jumping then stamina = current stamina value * jumping_stamina_depletor and a condition if stamina < jumping_stamina_depletor then cannot excecute jump code therefore you cannot jump.
Sprinting Mechanics Problems<< my player moves at constant speed diagonally but if sprinting diagonally then the player moves faster than the sprinting speed because the sprinting velocity and default speed add up when sprinting diagonally. I would like to be able to sprint in the -Z direction only and if I'm sprinting and pressing the other directions at the same time then those other direction's velocity must be 0 so they don't add to the sprint forward velocity when I run diagonally (which I won't be able to if the other axis's velocity are 0 which is desired). OR maybe just changing the default_speed velocity to sprint velocity on the -Z axis when running should solve it ? My player can sprint backwards and sideways which I would like to remove the ability to but if I press the sprint button which is SHIFT and moving left or right then it will go back to default_speed.
Here are some more problems I am facing though: I cannot make a stamina refiller that charges from 0-100 in 90seconds I haven't figured a way to deplete 100 stamina in 60 seconds if player is sprinting
Here is my code for now Edited**
extends KinematicBody
#Player Movements Variables
export var speed : float
export var default_speed : float = 1
export var sprint_speed : float = 17
export var acceleration : float = 2.5
export var air_acceleration : float = speed / 5
export var gravity : float = 9.81
export var terminal_velocity : float = 50
export var jump_velocity : float = 6.47
#Snap Parameters
var snap_direction = Vector3.DOWN
var snap_length = 2
var snap_vector = snap_direction * snap_length
#Player Run Timeout Variable
export var sprinting = false
#Stamina Mechanics & Multiplier Variables
var stamina : float = 100
var stamina_recovery_rate : float = 10 / 9
var jumping_stamina_depletor : float = 100 / 3
var sprint_stamina_depletor : float = 0.9
#Player Camera Angles Variables
export(float, 0.1, 1) var mouse_sensitivity : float = 0.1
export(float, -90, 0) var min_pitch : float = -23
export(float, 90, 0) var max_pitch : float = 23
#Vertical Speed Variable
var velocity : Vector3
#Referencing the Camera path
onready var CameraPivot = $CameraPivot
onready var camera = $CameraPivot/CameraBoom/Camera
#Mouse track
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
#Checks every frame to lock/unlock Mouse
func _process(delta):
if Input.is_action_just_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
#Mouse rotation X & Y axis
func _input(event):
if event is InputEventMouseMotion:
rotation_degrees.y -= event.relative.x * mouse_sensitivity
CameraPivot.rotation_degrees.x -= event.relative.y * mouse_sensitivity
CameraPivot.rotation_degrees.x = clamp(CameraPivot.rotation_degrees.x, min_pitch, max_pitch)
#Function that handles the physicss
func _physics_process(delta):
_handle_movement(delta)
#Character Movement Code
func _handle_movement(delta):
var direction = Vector3()
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
if Input.is_action_pressed("move_backward"):
direction += transform.basis.z
if Input.is_action_pressed("move_left"):
direction -= transform.basis.x
if Input.is_action_pressed("move_right"):
direction += transform.basis.x
#Acceleration & Speed Switch
var accel = acceleration if is_on_floor() else air_acceleration
if Input.is_action_pressed("sprint_forward") and not sprinting and stamina > 1:
sprinting = true
speed = sprint_speed
stamina = clamp(stamina * sprint_stamina_depletor, 0, 100)
else:
sprinting = false
speed = default_speed
velocity = velocity.linear_interpolate(direction * speed, accel * delta)
#Handles diagonal speed
direction = direction.normalized()
#Gravity Pull & Terminal Velocity
if velocity.y < terminal_velocity and is_on_floor():
velocity.y = -0.01
elif velocity.y < terminal_velocity:
velocity.y -= gravity * delta
else:
velocity.y = clamp(velocity.y - gravity, -terminal_velocity, terminal_velocity)
#Jump Mechanics
if stamina >= jumping_stamina_depletor and Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
stamina = clamp(stamina - jumping_stamina_depletor, 0, 100)
velocity = move_and_slide(velocity, Vector3.UP, true, 4, 0.78)
#Stamina_Recovery
stamina += delta