Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain velocity when jumping from moving platforms #31

Open
zvodd opened this issue May 11, 2023 · 0 comments
Open

Retain velocity when jumping from moving platforms #31

zvodd opened this issue May 11, 2023 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@zvodd
Copy link

zvodd commented May 11, 2023

If the player is standing on a moving platform, they will come to a halt in the air when jumping.
This is because air control causes deceleration towards 0 velocity, hence stopping dead in the air when there is no input.
I've been attempting to implement a fix in the Movement Controller. No luck so far...

I can make it work by removing all air control and simply keeping and applying the x & z velocity upon jumping.

I think, In theory its a matter of taking into account the velocity the time of jump and making the deceleration relative to that.

Splitting the air and ground integration may help:

# Called every physics tick. 'delta' is constant
func _physics_process(delta: float) -> void:
	input_axis = Input.get_vector(&"move_back", &"move_forward",
			&"move_left", &"move_right")
	
	direction_input()
	
	if is_on_floor():
		if Input.is_action_just_pressed(&"jump"):
			velocity.y = jump_height
			velocity_on_jump = Vector3(velocity.x, 0, velocity.z)
		accelerate_ground(delta)
	else:
		velocity.y -= gravity * delta
		accelerate_air(delta)
	
	move_and_slide()

func accelerate_ground(delta: float) -> void:
	# Using only the horizontal velocity, interpolate towards the input.
	var temp_accel: float
	var temp_vel := Vector3(velocity.x, 0, velocity.z)
	var target: Vector3 = direction * speed
	
	if direction.dot(temp_vel) > 0:
		temp_accel = acceleration
	else:
		temp_accel = deceleration
	
	temp_vel = temp_vel.lerp(target, temp_accel * delta)
	
	velocity.x = temp_vel.x
	velocity.z = temp_vel.z

func accelerate_air(delta: float) -> void:
	# Using only the horizontal velocity, we just leave it untouched I guess
	var temp_accel: float
	var temp_vel := Vector3(velocity.x, 0, velocity.z)
		
	velocity.x = temp_vel.x
	velocity.z = temp_vel.z
@Whimfoome Whimfoome added bug Something isn't working good first issue Good for newcomers labels May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants