Trouble adjusting player timescale
Asked Answered
M

1

0

So I want to adjust timescale only for the player movement.

The basic integration method used is:

velocity += acceleration * deltaTime * timeScale
position += velocity * deltaTime * timeScale 

For calculating the position I use:
move_and_slide(velocity * TimeScale, Vector2.UP)

delta is omitted from the arguments, because I understand it is added to the calculation automatically.

The movement doesn't work as it should:

  • Downward velocity does not change from gravity being added, it is constant and too slow
  • Jump barely gets off the ground
  • horizontal movement is constant and slow as well

Is there something I'm doing wrong? I was hoping that move_and_slide could be used here.

The full code:

func physics_process(player, input, delta):
	#apply gravity
	player.velocity.y += player.moveData.GRAVITY * delta * player.TimeScale
	player.velocity.y = min(player.velocity.y, 3000)
	
	if input.x == 0:
		player.velocity.x = move_toward(player.velocity.x, 0, player.moveData.FRICTION * delta * player.TimeScale)
		if player.is_on_floor():
			if player.velocity.x == 0:
				player.AnimatedSprite.animation = "Idle"
			else:
				player.AnimatedSprite.animation = "Stop"
	else:
		player.velocity.x = move_toward(player.velocity.x, player.moveData.MAX_SPEED * input.x, player.moveData.ACCELERATION * delta * player.TimeScale)
		player.AnimatedSprite.flip_h = input.x < 0
		if player.is_on_floor():
			player.AnimatedSprite.animation = "Run"		
			
	
	if !player.is_on_floor():
		if player.nextToWall and Input.is_action_just_pressed("ui_up"):
			return player.States.WallJump
		if player.velocity.y <= 0:
			player.AnimatedSprite.animation = "Jump"
			if Input.is_action_just_released("ui_up") and player.velocity.y < 0  and player.jumping:
				player.jumping = false
				player.velocity.y = 0	
		if player.velocity.y  > 0:
			player.AnimatedSprite.animation = "Fall"
			player.velocity.y += player.moveData.ADDITIONAL_FALL_GRAVITY * delta * player.TimeScale
	else:
		if Input.is_action_just_pressed("ui_up"):
			player.jumping = true
			player.velocity.y = player.moveData.JUMP_FORCE
	
	player.velocity = player.move_and_slide(player.velocity * player.TimeScale, Vector2.UP)
	return null

Edit: This works fine as long as the timeScale is 1.0, any lower than that and the issues appear

Mullein answered 14/12, 2022 at 17:33 Comment(0)
M
0

Looks like the returning velocity from move_and_slide() needs to be adjusted, like:
player.velocity = player.move_and_slide(player.velocity * player.TimeScale, Vector2.UP) / player.TimeScale

This seems to have fixed my issue.

Mullein answered 14/12, 2022 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.