Godot Version
godot 4.3
Question
I found few tutorials and the move_toward() method seemed fine,
velocity = velocity.move_toward(speed * direction, acceleration) it takes two argument (maybe more) and here changes the velocity from what it was to 'speed * direction' by increments of 'acceleration' I'm having trouble with jumping as when pressing required input the player gains in altitude but falls very slowly, whenever I tried changing things a bit it went wrong some other way like only falling when moving or not fallin at all
extends CharacterBody2D
var speed = 200.0
var jump = -400.0
var acceleration = 40
var deceleration = 80
var direction = Vector2.ZERO
var gravity = 800
func input():
var direction: Vector2 = Vector2.ZERO
direction.x = Input.get_axis("ui_left", "ui_right")
direction = direction.normalized()
return direction
func _physics_process(delta):
# Add the gravity.
velocity.y += gravity * delta
# Movement
var direction: Vector2 = input()
if direction != Vector2.ZERO:
velocity = velocity.move_toward(speed * direction, acceleration)
else:
velocity = velocity.move_toward(Vector2.ZERO, deceleration)
# Handle jump.
velocity.y += gravity * delta
if Input.is_action_just_pressed("ui_up"):
velocity.y = jump
move_and_slide()
I am desperate to understand this problem but another method to implement “smooth” movement for a 2d platformeris appreciated.