Character teleporting while flip_h is set to true

Godot Version

Godot_v4.2.2-stable_mono_win64

Question

My character is not staying while flipping it’s direction. I have this code to flip it:

	if velocity.x > 1:
		Player.flip_h = false
	elif velocity.x <  -1:
		Player.flip_h = true

Check video for visuals on issue:

Does anyone know why it’s not staying in place? I’ve done this many times and I haven’t encountered this issue before.

In your sprite’s image files, is the character 's body perfectly centered within each image on the x-axis? If it isn’t centered, then flipping the entire image horizontally will result in it shifting across the center to the other side of the image. I think this is the most likely explanation.

2 Likes

Hmmm, thanks. That will be the only logical reason why fr. But is there a way to key it to it’s initial position after flipping through code?

You mean fixing it programmatically, without changing the original image?

That would probably require storing an x-offset for each image to indicate how much to shift it when flipping. And depending whether your character is facing left or right, adding or subtracting that value from the sprite’s x-position. Or if each image is offset by the same amount, storing a single x-offset and using that for all of them.

But either way, it’s adding complexity and effort for no real gain. Counting pixels and storing them for every image.

Centering the original images would require the least effort. Especially when you start adding more objects that can also face both directions.

1 Like

That does sound like a lot of work :joy:. Thanks, I might just change the sprite entirely