0 votes

Hello, I recently implemented easing so i can smooth out the movement of my character. All and all everything is working well, except for when my character is falling. Basically, the speed of the fall is increased when the player moves mid-air. I have no idea how to balance it out with my current code. Any advice is greatly appreciated. Here is the relevant code:

const MAX_SPEED = 25
const LOOK_AROUND_SPEED = 0.2
const GRAVITY_ACCELERATION = 30

var input_move: = Vector3()
var gravity_local: = Vector3()
var snap_vector: = Vector3()
var velocity = Vector3.ZERO
export(int) var jump_speed: = 10

disable_movement = false


func _physics_process(delta):   
    input_move = _get_directions()  

    if input_move != Vector3.ZERO:
        velocity = input_move * MAX_SPEED

    if is_on_ceiling():
        gravity_local = Vector3.ZERO

    snap_vector = Vector3.DOWN

    if not is_on_floor():
        gravity_local += Vector3.DOWN * GRAVITY_ACCELERATION * delta
    else:   
        gravity_local = Vector3.ZERO

    if is_on_floor():
        snap_vector = -get_floor_normal()

    if (Input.is_action_just_pressed("jump") and is_on_floor() and !isGrapplingActive) or isGrappleHit:
        #when player collides with an enemy -> jump
        if isGrappleHit:
            jump_speed *= 2
        snap_vector = Vector3.ZERO
        gravity_local = Vector3.UP * jump_speed 
        isGrappleHit = false
        jump_speed = 10

    # SHOOT GRAPPLE
    # basically a method which targets an enemy node and flies towards it.
    if Input.is_action_just_pressed("grapple"):
        if !isGrapplingActive:
            grapple_target()

    if isGrapplingActive:
        update_grappling()
    else:
        velocity = move_and_slide_with_snap(velocity + gravity_local , snap_vector, Vector3.UP)
        if shotgun_velocity == Vector3.ZERO:
            disable_movement = false
            var ease_curve = -0.1
            if not is_on_floor():
                ease_curve = -0.5
            var slowdown = ease(min(delta/0.4, 1.0), ease_curve)
            velocity -= move_and_slide_with_snap((velocity * slowdown) + gravity_local, snap_vector, Vector3.UP)
            print(velocity)
        else:
            velocity = Vector3.ZERO
        #velocity -= velocity * slowdown

func _get_directions() -> Vector3:
   var z: float = ( Input.get_action_strength("backward") - Input.get_action_strength("forward") )
   var x: float = ( Input.get_action_strength("right") - Input.get_action_strength("left") )
   if isGrapplingActive or disable_movement:
       x = 0
       z = 0
   return transform.basis.xform(Vector3(x, 0, z)).normalized()
Godot version 3.5
in Engine by (23 points)

1 Answer

0 votes

Ok, seems I fixed the problem. I simply changed the line for the velocity by only changing the x and z axis. Basically went from:

if input_move != Vector3.ZERO:
    velocity = input_move * MAX_SPEED

To this:

if input_move != Vector3.ZERO:
    velocity.x = input_move.x * MAX_SPEED
    velocity.z = input_move.z * MAX_SPEED

And it seems to be fine. the y axis is only affected by the gravity_local variable.

by (23 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.