Btan2 / Q_Move

Quake-like controller for Godot
GNU General Public License v3.0
89 stars 8 forks source link

Player doesn't gain full speed until it isfully standing #2

Closed Leakbang closed 1 year ago

Leakbang commented 2 years ago

This ensures that the player is fully standing before giving the speed back.

The - 0.1 is a duct tape solution. Originally, I had if collider.shape.height >= PLAYER_HEIGHT and even though both values were 3.6, it would output as false. I also tried storing the player height in another variable and using == but that didn't work as well. I also added a walking toggle variable that can be toggled with CapsLock.

Btan2 commented 1 year ago

Sorry for the long delay. Thanks for bringing this up, I realised I put the global movespeed var inside _input() which was dumb because that meant movespeed only updated when a button (any button!) was pressed. So I shifted the movespeed calc into _physics_process() and made sure it executes before the ground check and movement funcs. I also implemented a small change which makes sure the player is grounded while the collider shape height is being altered. If you like the player falling to the ground when they crouch you can remove it, but I think it looks much more smooth and it reduces the risk of unexpectedly sliding off sharp edges while moving to a crouch. This will be added to the latest version of the project which I will be uploading very, very soon, but here is the code for the fix for now:

# New global vars
# Assign in _input(), do something like: crouch_press = Input.is_action_pressed("crouch")
var crouch_press : bool = false 
var walk_press : bool = false

#-------------------------------------------------------
func _physics_process(delta):
    deltaTime = delta

        movespeed = WALKSPEED if walk_press else MAXSPEED
    if crouch_press:
        movespeed = clamp(movespeed/2, WALKSPEED/2, WALKSPEED)

        # crouch()
        # categorize_position()
        # etc.

#-------------------------------------------
func crouch():
    var crouch_speed = 20.0 * deltaTime

    if crouch_press:
        # snap to crouch height while falling
        if state == FALLING:
            collider.shape.height = CROUCH_HEIGHT
        elif collider.shape.height > CROUCH_HEIGHT:
            collider.shape.height -= crouch_speed               

            # push down to remain grounded
            global_transform.origin[1] -= min(crouch_speed, abs(CROUCH_HEIGHT - collider.shape.height))
            #p_reset = true
    else:
        # speed should be nerfed while moving from crouch
        movespeed *= collider.shape.height/PLAYER_HEIGHT 
                #movespeed = MOVESPEED

        if collider.shape.height < PLAYER_HEIGHT:
                        #movespeed = WALKSPEED
            var pm_trace = Trace.new(collider.shape, ENV_MASK, get_world())

            # check for overhead collision
            var start = global_transform.origin
            var stop = global_transform.origin + Vector3.UP * crouch_speed
            var trace = pm_trace.motion(start, stop)            

            # nothing above, increase shape height
            if trace[0] == 1.0: #and trace[1] == 1.0:
                collider.shape.height += crouch_speed

    collider.shape.height = clamp(collider.shape.height, CROUCH_HEIGHT, PLAYER_HEIGHT)

Again, sorry for taking so long but free time is difficult to come by as of late.