scale randomly swiching

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kaifungamdev

I have a frog object that’s always supposed to face the player but it will randomly switch.
here’s my code:

func _physics_process(delta):
	vel.y += gravity
	jump_timer += delta

	if jump_timer >= JUMP_DELAY and playerneer:
		direction = sign(player.position.x - position.x)
		scale.x *= direction
		jump()
		jump_timer = 0.0
		in_air = true
	

	set_velocity(vel)
	move_and_slide()
	if in_air and is_on_floor():
		vel.x = 0
		in_air = false

in this video in the last jump the frog does not flip any reason why?

:bust_in_silhouette: Reply From: PickDough

Hi!

You can figure out the direction of frog based on the sign of both its and players x position. For example, if player is further to the right from the frog then the difference would be positive and we keep it’s right direction (direction = 1). (Assuming default frog is looking to the right)

You can use your code as well but you have to check for x component of position not the whole vector.

direction = sign(player.position.x - position.x)
scale.x *= direction
jump()
jump_timer = 0.0
in_air = true

that didn’t solve my problem though but it did make my code shorter so thanks.
I should have added a video (which I now did) and explained my problem in more detail

kaifungamdev | 2023-05-22 20:52

How often do you check for this direction? Perhaps the frog remembered previous position of player in last jump and didn’t account for new. Could you provide whole code with timer and where this check of postion is performed.

PickDough | 2023-05-22 22:07

I’ve updated my question. it checks before it jumps, I’ve tried moving it out of the timer but it will just flip randomly

kaifungamdev | 2023-05-23 13:34