Collisionshape shift when sprite flip_h

Godot Version

Godot_v4.2.1

Question

Okay so I’m having trouble with the flipping of hitbox when flip_h is active.

So here is my func for animating movement:

func play_move_anim(direction):
var dir = current_dir
if attack_ip == false:

	if player_state == "idle":
		if current_dir == "left":
			$Sprite2D.flip_h = true
			$Sprite2D/AnimationPlayer.play("idle")
		else:
			$Sprite2D.flip_h = false
			$Sprite2D/AnimationPlayer.play("idle")
			
	if player_state == "walking":
		if direction.x == 1:
			current_dir = "right"
			$Sprite2D.flip_h = false
			$Sprite2D/AnimationPlayer.play("walk")
			
		if player_state == "walking":
			if direction.x == -1:
				current_dir = "left"
				$Sprite2D.flip_h = true 
				$Sprite2D/AnimationPlayer.play("walk")
			if direction.y == -1:
				if current_dir == "right":
					$Sprite2D.flip_h = false
					$Sprite2D/AnimationPlayer.play("walk")
					
				elif current_dir == "left":
					$Sprite2D.flip_h = true
					$Sprite2D/AnimationPlayer.play("walk")
					
			if direction.y == 1:
				if current_dir == "right":
					$Sprite2D.flip_h = false
					$Sprite2D/AnimationPlayer.play("walk")
					
				elif current_dir == "left":
					$Sprite2D.flip_h = true
					$Sprite2D/AnimationPlayer.play("walk")
					
				
			if direction.x > 0.5 and direction.y < -0.5:
				current_dir = "right"
				$Sprite2D.flip_h = false
				$Sprite2D/AnimationPlayer.play("walk")
			if direction.x > 0.5 and direction.y > 0.5:
				current_dir = "right"
				$Sprite2D.flip_h = false
				$Sprite2D/AnimationPlayer.play("walk")
			if direction.x < -0.5 and direction.y > 0.5:
				current_dir = "left"
				$Sprite2D.flip_h = true
				$Sprite2D/AnimationPlayer.play("walk")
			if direction.x < -0.5 and direction.y < -0.5:
				current_dir = "left"
				$Sprite2D.flip_h = true
				$Sprite2D/AnimationPlayer.play("walk")

and this is my _physics_process func:

func _physics_process(delta):

if $Sprite2D.flip_h == false:
	$Sprite2D/hitbox.position.x *= 1
elif $Sprite2D.flip_h == true:
	$Sprite2D/hitbox.position.x *= -1

player_movement(delta)
attacking(delta)

so it works…sort of. When flip_h is active it seems the Area2d(hitbox) is rapidly switching from its default position to the new position at x *= -1. Occasionally when flipping orientation of the Sprite, the hitbox will get stuck on the wrong side of the sprite.

I dont know what Im doing wrong as it seems to partially work, just not as intenden.

have you tried not to put collision shape 2d as a child in this sprite2d?

yes I’ve tried that and it has the same results.

This is my very first project so likely im just missing something rather simple, but I cant seem to figure out what.

thanks for the input though

can you show your scene tree with the sprite2d that’s flipped and collision shape 2d hitbox

I hope this is what you asked for

yes it’s what i wanted to see
the hitbox function should be controlling your collisionShape shift, not the sprite flip_h
if you mean another collision shape 2d below the animation player, then it has nothing to do with your sprite flipping or not
but how do you “shift” the collisionshape2d below the animation player?

If i understand your question correctly, I assing a var current_dir on directional input. so either “left” or “right” ( for now) and then if current_dir = “left”: $sprite2d.flip_h

image

yes but those flip_h lines shouldnt make the hitbox change, only the current_dir will be used in the hitbox() method.
so your $hitbox.position.x*=-1 might be the issue if it’s shifted, try check the hitbox node position, is it at 0x, 0y?

also this method will be incorrect if you pressed “left” twice, it will “flipped” to the right side because you *=-1 it

okay yeah I see what your saying.

So basically the tapping left twice will make the function run again thus again switching the sides.

would it then be smarter to say:

if $Sprite2d.flip_h == true:
$hitbox.pos.x

instead of depending it on the directional input i mean.

sorry if my ignorance is annoying. Thanks for the help though

also no:

" so your $hitbox.position.x*=-1 might be the issue if it’s shifted, try check the hitbox node position, is it at 0x, 0y?"

the node is not at 0x,0y

the problem remains, and I cant even send a screenshot because it switches so that in any single frame the hitbox appear on only one side

Solved it myself, and feel rather dumb about not having seen it prior.

I just set the position.x = whatever it is in the transform and the negative of that to flip sides on state needed.

you input helped me see it though so thanks

nice to see it solved