Godot Version
4.6
Question
I was making a WASD moment for my FPS game ( ONE MAG ), i started working on this project few days ago.
So the problems are :
When i press jump and release w the player while in the air decelerate to 0.0, stops on the position and come down.
The player when on floor decelerating, do not decelerate in diagonal direction.
The script :
extends CharacterBody3D
const SPEED = 10
const JUMP_VELOCITY = 7
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)
const mass = 5
func _ready(): #cursor capture
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if event is InputEventMouseMotion:
rotation.y -= event.screen_relative.x * 0.001
%Camera3D.rotation_degrees.x -= event.screen_relative.y * 0.1
%Camera3D.rotation_degrees.x = clamp(
%Camera3D.rotation_degrees.x, -60, 60
)
elif event.is_action_pressed(“ui_cancel”):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
elif event.is_action_pressed(“ui_accept”):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
var decel = SPEED * 3.5 * delta
if not is_on_floor():
velocity.y -= gravity * delta
else:
velocity.y = move_toward(velocity.y, 0.0, decel)
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y += JUMP_VELOCITY
var input_dir = Input.get_vector("move_left","move_right","move_forward","move_back")
var direction = ( transform.basis * Vector3(input_dir.x, 0.0 , input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else :
velocity.x = move_toward(velocity.x, 0.0, decel)
velocity.z = move_toward(velocity.z, 0.0, decel)
move_and_slide()
Indiegenes_Games:
velocity.x = move_toward(velocity.x, 0.0, decel)
velocity.z = move_toward(velocity.z, 0.0, decel)
This deceleration is favoring diagonal as the X and Z individually both move by devel rather than a combined movement of devel. Split your movement into horizontal and vertical components so you can use Vector3’s move_toward
var vertical_velocity := velocity.project(up_direction)
var horizontal_velocity := velocity - vertical_velocity
if Input.is_action_just_pressed("jump") and is_on_floor():
vertical_velocity += up_direction * JUMP_VELOCITY
horizontal_velocity = horizontal_velocity.move_toward(direction * SPEED, decel)
velocity = horizontal_velocity + vertical_velocity # recombine
move_and_slde()
1 Like
I tried it but somehow the moment become very slow !
extends CharacterBody3D
const SPEED = 10
const JUMP_VELOCITY = 7
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
const mass = 5
func _ready(): #cursor capture
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
# PROBLEM AT (5,1),(6,7) AND UPPER MID😭
func _unhandled_input(event):
if event is InputEventMouseMotion:
rotation.y -= event.screen_relative.x * 0.001
%Camera3D.rotation_degrees.x -= event.screen_relative.y * 0.1
%Camera3D.rotation_degrees.x = clamp(
%Camera3D.rotation_degrees.x, -60, 60
)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
elif event.is_action_pressed("ui_accept"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
var decel = SPEED * 3.5 * delta
var vertical_velocity := velocity.project(up_direction)
var horizontal_velocity := velocity - vertical_velocity
if not is_on_floor():
vertical_velocity.y -= gravity * delta
else:
vertical_velocity.y = move_toward(vertical_velocity.y, 0.0, decel)
if Input.is_action_just_pressed("jump") and is_on_floor():
vertical_velocity += up_direction * JUMP_VELOCITY
var input_dir = Input.get_vector("move_left","move_right","move_forward","move_back")
var direction = ( transform.basis * Vector3(input_dir.x, 0.0 , input_dir.y)).normalized()
horizontal_velocity = horizontal_velocity.move_toward(
direction, decel )
velocity = vertical_velocity + horizontal_velocity
move_and_slide()
You must multiply the direction by SPEED or you will move at one unit per second
2 Likes
The diagonal Deceleration issue resolved now the only issue remains is the Momentum in Jump issue.
What do you want to happen?
i want a inertia in Jump !
amarc
February 8, 2026, 4:45pm
9
Reduce the decel value when the player is in the air. You can reduce it to zero to have total horizontal inertia or keep a low value so it still decelerates a little, but not as much as on the ground.