- Edited
Hello !
I'm trying to make a 3d game with smooth movement (friction and acceleration) and so I need the player to "move less" when he is in the air. So I tried to make it like this :
if not is_on_floor():
velocity.y -= gravity * delta
friction = 0.025
acceleration = 0.01
else:
friction = 0.1
acceleration = 0.25
I do it the same way in 2D for having smooth jump / movement
But sadly it's not working realy well :
https://youtu.be/5c7FrkIIUwE
It's the first time i'm doing a game in 3d so I don't how to take/fix the issue
Thx for helping me
Here is the whole code by the way
extends CharacterBody3D
@onready var main_camera = $MainCamera
#Movement var
var SPEED = 0
var MAX_SPEED = 10
const JUMP_VELOCITY = 4.5
const gravity = 10
var acceleration = 0.25
var friction = 0.1
#Camera var
var camera_rotation = Vector2(0,0)
var mouse_sensitivity = 0.0025
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
## MOVEMENT
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
friction = 0.025
acceleration = 0.01
else:
friction = 0.1
acceleration = 0.25
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
SPEED += acceleration
if SPEED > MAX_SPEED:
SPEED = MAX_SPEED
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
#velocity.x = move_toward(velocity.x, 0, SPEED)
#velocity.z = move_toward(velocity.z, 0, SPEED)
velocity.x = lerp(velocity.x, 0.0, friction)
velocity.z = lerp(velocity.z, 0.0, friction)
SPEED = 0
move_and_slide()
func _input(event):
if Input.is_action_just_pressed("echap"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
##CAMERA
if event is InputEventMouseMotion:
var mouse_event = event.relative * mouse_sensitivity
camera_look(mouse_event)
func camera_look(Movement: Vector2):
camera_rotation += Movement
camera_rotation.y = clamp(camera_rotation.y,-1.5,1.2)
transform.basis = Basis()
main_camera.transform.basis = Basis()
rotate_object_local(Vector3(0,1,0), - camera_rotation.x) #first rotate y
main_camera.rotate_object_local(Vector3(1,0,0), - camera_rotation.y) #rotate x