GarbajYT / godot_updated_fps_controller

An updated basic fps controller for Godot 3.3 and 4.0
MIT License
240 stars 49 forks source link

jumping vs ceiling #6

Open 3RUN opened 3 years ago

3RUN commented 3 years ago

Hey! Thank you for your tutorials! They really help me a lot in migrating to Godot. One small thing I would like to suggest (as enhancement) is to stop vertical velocity if character controller hits ceiling (while jumping). This is very easy to implement, but would be great if it would be included into the code. I didn't fork the repo so sorry for not pull requesting this one.

Keep up the great tutorials!

func _physics_process(delta):
    #get keyboard input
    direction = Vector3.ZERO
    var h_rot = global_transform.basis.get_euler().y
    var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
    var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
    direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized()

    #jumping and gravity
    if is_on_floor():
        snap = -get_floor_normal()
        accel = ACCEL_DEFAULT
        gravity_vec = Vector3.ZERO
    else:
        snap = Vector3.DOWN
        accel = ACCEL_AIR
        gravity_vec += Vector3.DOWN * gravity * delta

    if Input.is_action_just_pressed("jump") and is_on_floor():
        snap = Vector3.ZERO
        gravity_vec = Vector3.UP * jump

    #hit the ceiling ?
    if is_on_ceiling():
        gravity_vec = Vector3.ZERO

    #make it move
    velocity = velocity.linear_interpolate(direction * speed, accel * delta)
    movement = velocity + gravity_vec

    move_and_slide_with_snap(movement, snap, Vector3.UP)
revdeluxe commented 3 years ago

Depends on what kind of movement this will work if the game is like CS:GO. hitting the ceiling and losing velocity will do. great fix...

One thing though normalizing movement will result into 0.75 on both NW(UP+LEFT) and NE(UP+RIGHT). just to make a analog like movement. direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized() instead of having chromatic 1,1.

perhap enabling it if what kind of controls is the player using...