Hello,
Im triying to make a good sensing player controller for a 2d platformer, but I can’t find a way of making it feel smooth. For the moment, this is my code:
Current code (GDScript)
extends CharacterBody2D
var Gravity:float = ProjectSettings.get("physics/2d/default_gravity")*1.2
@export var HAcel := 950
@export var HMaxSpeed := 300
@export var JumpForce := 450
@export var CoyoteTime := 0.15
var RoundToStopFloor := HAcel*0.3
var RoundToStopAir := HAcel*0.1
enum MoveStates{
Walk,
Jump
}
var CurrentMoveState:int = MoveStates.Walk
@onready var CoyoteTimer = $Timer
func _physics_process(delta):
velocity.y += Gravity*delta
if velocity.y >0: #This will make the fallling faster (I read somewhere that it makes the movement feel better)
velocity.y += Gravity*delta
velocity.y = min(500, velocity.y)
var HMovement = Input.get_axis("game_left", "game_rigth")
var IsOnFloor := is_on_floor()
if HMovement>0:
$Sprite2D.flip_h = false
elif HMovement<0:
$Sprite2D.flip_h = true
if HMovement*velocity.x<0: #Change run orientation
velocity.x = -velocity.x
velocity.x = lerp(velocity.x, 0.0, 0.5)
if abs(velocity.x) <HMaxSpeed and HMovement != 0: #Increase velocity
velocity.x += HAcel*delta*HMovement
elif HMovement == 0 and abs(velocity.x)>RoundToStopFloor and IsOnFloor: #Decrease velocity in floor
velocity.x += HAcel*delta*(-velocity.x/abs(velocity.x))
elif abs(velocity.x) <=RoundToStopFloor and IsOnFloor: #Truncate velocity on earth
velocity.x = 0
elif abs(velocity.x) <=RoundToStopAir and not IsOnFloor:
velocity.x = 0
if Input.is_action_just_pressed("Jump") and CoyoteTimer.time_left>0:
CurrentMoveState = MoveStates.Jump
velocity.y = -JumpForce
if IsOnFloor:
CoyoteTimer.wait_time = CoyoteTime
CoyoteTimer.start()
move_and_slide()
With this code, the player jumps and walk feels weird and I don’t know what to do to fix it and make it feel good (A good platforms game).
Do anyone know how to make it feel good?
Thanks