my character is x axis resistant

im making my first attempt at a 3d godot game in the 4.5 vercion so i used a premade character controller wich is really good exept for one conflict that i can.t figure out how to fix it makes my character only take y knockback or more accurately resist a lot of x knockback (wich also means if i put enough for the x knockback to look good if you jump you end up in a new space program)

i use this code (the damage and dir come from the enemy, the pushback is a fixed variable wich is 8.0):

func hit(damage, dir):

hp -= damage

velocity = dir \* pushback

wich works but i didn.t know that since this code:

if can\_move:

	var input\_dir := Input.get\_vector(input\_left, input\_right, input\_forward, input\_back)

	var move\_dir := (transform.basis \* Vector3(input\_dir.x, 0, input\_dir.y)).normalized()

	if move\_dir:

		velocity.x = move\_dir.x \* move\_speed

		velocity.z = move\_dir.z \* move\_speed

	else:

		velocity.x = move\_toward(velocity.x, 0, move\_speed)

		velocity.z = move\_toward(velocity.z, 0, move\_speed)

else:

	velocity.x = 0

	velocity.y = 0



\# Use velocity to actually move

move\_and\_slide()

is causing a conflict that slows down ALL my x speed so knockback is affected as well i would rather keep this controller since its really good apart from this part and had some bad luck when i tried using others or making others so i wanted to ask for help on how to fix this issue

the full code:

https://pastebin.com/D5HBpuBA

Instead of setting the velocity to 0 directly, you can lerp the value down to 0

    # Apply desired movement to velocity
    if can_move:
        var input_dir := Input.get_vector(input_left, input_right, input_forward, input_back)
        var move_dir := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
        if move_dir:
            velocity.x = move_dir.x * move_speed
            velocity.z = move_dir.z * move_speed
        else:
            velocity.x = move_toward(velocity.x, 0, move_speed)
            velocity.z = move_toward(velocity.z, 0, move_speed)
    else:
        velocity.x = lerp(velocity.x, 0, delta)
        velocity.y = lerp(velocity.y, 0, delta)

Then you can change how fast the change in speed occurs by multiplying delta by something:
delta * .5 would make the velocity decay slower delta * 5 would make it faster.

Note tho that this would also make your character “slide” when controlling

Adding to what @vonpanda states, check how the can_move condition is calculated to avoid jumps and weird movements. I imagine you will want that effect having a duration or something.

1 Like