Hello so I'm having trouble with my code. I'm trying to implement a Gravity Mechanic where when the player is on the floor the gravity is "-0.01" which means he is just anchored on the ground.
However I want him when he is not on the ground aka falling to fall at a rate of
y_velocity = y_velocity - gravity.
But gravity is : Gravity = sqrt(2 x h / g) where h is height, I put 1 in my case so 2 x 1 = 2 and g is the acceleration of gravity where g is 9.81m/s
So basically the higher you are from the ground, the faster you'd accelerate downwards until you hit the ground or reach terminal velocity 50m/s.
When I run my code, the player can't fall and I was wondering why.
Down below is the code. I am new to programming so I apologize if my mistakes are very basic.~~~~
extends KinematicBody
#Player Movements Variables
export var speed : float = 8
export var acceleration : float = 2.5
export var air_acceleration : float = 1
export var gravity : float
export var acceleration_of_gravity : float = 9.81
export var terminal_velocity : float = 50
export var jump_velocity : float = 35
#Player Camera Angles Variables
export(float, 0.1, 1) var mouse_sensitivity : float = 0.3
export(float, -90, 0) var min_pitch : float = -23
export(float, 90, 0) var max_pitch : float = 23
#Vertical Speed Variables
var velocity : Vector3
var y_velocity : float
#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 physics
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
#Handles diagonal speed
direction = direction.normalized()
#Acceleration switch in air/ground
var accel = acceleration if is_on_floor() else air_acceleration
velocity = velocity.linear_interpolate(direction * speed, accel * delta)
#Gravity
var output_gravitation : float
output_gravitation = 2 / acceleration_of_gravity
gravity = sqrt(output_gravitation)
#Gravity Pull
if y_velocity < terminal_velocity and is_on_floor():
y_velocity = -0.01
elif y_velocity < terminal_velocity:
y_velocity = y_velocity - gravity
else:
y_velocity = clamp(y_velocity - gravity, -terminal_velocity, terminal_velocity)
#Jump Mechanics
if Input.is_action_just_pressed("jump") and is_on_floor():
y_velocity = jump_velocity
velocity = move_and_slide(velocity, Vector3.UP)
y_velocity = velocity.y
