#Acceleration and Deceleration for Characterbody3D - how do I implement it?

1 messages · Page 1 of 1 (latest)

normal summit
#

Here's some failed attempts puzzled together from tutorials

    if not momentum == 0:
        momentum -= move_decel
    
func _physics_process(delta):
    velocity.y += -gravity * delta
    getPlayerInput(delta)

    move_and_slide()

func getPlayerInput(delta):

    velocity.y = 0     #y set to zero because gravity takes care of it
    var input_dir = Input.get_vector("player_left", "player_right", "player_up", "player_down") # camera doesnt follow player
    var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()         # camera doesnt follow player
#    var input = Input.get_vector("player_left", "player_right", "forward", "back")     # camera follows player
#    var dir = Vector3(input.x, 0, input.y).rotated(Vector3.UP, spring_arm.rotation.y)    # camera follows player 
    velocity = lerp(velocity, direction * move_speed, move_accel * delta) #acceleration
    #momentum = velocity>

# Jump and Air:
    if not is_on_floor():
        # velocity = move_speed_air ?
        velocity += get_gravity() * delta
    #else = move_speed_floor ?
    
    
    if Input.is_action_just_pressed("player_jump") and is_on_floor():
        velocity.y = JUMP_VELOCITY
        
    if Input.is_action_just_released("player_jump") and jump_time_min:
        #velocity.y = JUMP_VELOCITY / 4
        velocity.y = 0.0
        jump_cancel = true
        pass
        

    var input_dir = Input.get_vector("player_left", "player_right", "player_up", "player_down")
    var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    if direction:
        velocity.x = direction.x * move_speed
        velocity.z = direction.z * move_speed
    else:
        velocity.x = direction.x * moveDecel(velocity.x)
        velocity.z = direction.z * moveDecel(velocity.z)

    move_and_slide()```
#
    if not is_on_floor():
        # velocity = move_speed_air ?
        velocity += get_gravity() * delta
    #else = move_speed_floor ?
    
    
    if Input.is_action_just_pressed("player_jump") and is_on_floor():
        velocity.y = JUMP_VELOCITY
        
    if Input.is_action_just_released("player_jump") and jump_time_min:
        #velocity.y = JUMP_VELOCITY / 4
        velocity.y = 0.0
        jump_cancel = true
        pass
        

    var input_dir = Input.get_vector("player_left", "player_right", "player_up", "player_down")
    var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    if direction:
        velocity.x = direction.x * move_speed
        velocity.z = direction.z * move_speed
    else:
        velocity.x = direction.x * moveDecel(velocity.x)
        velocity.z = direction.z * moveDecel(velocity.z)

    move_and_slide()```
pulsar ruin
#

This is more of a #1235175950241628273 question. You can view the post guidelines by clicking the button next to New Post.

normal summit
#

oh ok, sorry, sure i will post there

pulsar ruin
#

Also, don't use lerp() for acceleration, use Vector3.move_toward() instead. The reasons are a bit complicated, but as a rule of thumb the "from" and "to" parameters should be constant if you are using lerp and you can't do that for acceleration..

normal summit
pulsar ruin
#

Yeah, it's a common mistake. I only found out that it was one by looking at old C++ posts on stackoverflow.

#

Even Godot's docs make it.

normal summit