Hello everyone. I know this is a popular topic but I'm pretty new to Godot and programming in general, so bear with me please.

My wife and I decided to make a game and from watching tutorials on youtube we've learned we can flip our AnimatedSprite using flip_h.

But the thing is that this won't flip the collision shape and although I've read about scale.x -1, and tried to look at my code and figure out where to put it, I have no idea where (or how) to implement that to my code.

I'm pasting my code below and adding a screenshot of my layers order, if there is anyone patient enough to help me I will be extremely grateful.

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMPFORCE = -288
const FLOOR = Vector2(0, -1)

var velocity = Vector2()
var on_ground = false


func _physics_process(delta):
	if Input.is_action_pressed("p_right"):
		velocity.x = SPEED
		$AnimatedSprite.play("walk")
		$AnimatedSprite.flip_h = false
	elif Input.is_action_pressed("p_left"):
		velocity.x = -SPEED
		$AnimatedSprite.play("walk")
		$AnimatedSprite.flip_h = true
	else:
		velocity.x = 0
		if on_ground == true:
			$AnimatedSprite.play("idle")
		
	if Input.is_action_just_pressed("p_jump"):
		if on_ground == true:
			velocity.y = JUMPFORCE
			on_ground = false
	
	velocity.y += GRAVITY

	if is_on_floor():
		on_ground = true
	else:
		on_ground = false
		if velocity.y < -1:
			$AnimatedSprite.play("jump")
		else:
			$AnimatedSprite.play("fall")

	velocity = move_and_slide(velocity, FLOOR)

	velocity.x = lerp(velocity.x,0,0.2)

If you are curious why my character has 3 collision shapes: My character, a Llama, has 3 collision shapes. I tried to add one CollisionPolygon2D but that didn't work as I wanted

Thank you!

While I don't do 2D, I think you would just need to replace the flip_h with the scale adjustment. The reason being the scale is changing the parent, and thus its children (including the sprite) will all be flipped:


if Input.is_action_pressed("p_right"):
	velocity.x = SPEED
	$AnimatedSprite.play("walk")
	scale.x = 1
elif Input.is_action_pressed("p_left"):
	velocity.x = -SPEED
	$AnimatedSprite.play("walk")
	scale.x = -1

If you play around with lerp or use the animationplayer you could probably make the flip look a little more paper Mario-ey if you wanted too

Additionally, if scaling causes issues or you want to go a different route, you can also have collision shapes for each side, and then use the disabled property on each collision shape node so the proper shapes are enabled for the side the character is facing and disabled for the side the character is not facing. Its a bit of a hassle though, so I would suggest using scaling like @Bimbam suggested if you can.

Edit: Also, welcome to the forums!

@Bimbam How nice! Thank you for the lerp suggestion, I will try that, I like it =)

@TwistedTwigleg Oh I like that! I will try Bimbam's suggetion first but I will also look into your idea!

Thank you very much folks :D

EDIT:

I just wanted to let you guys know that I experimented with both suggestion and sadly the scale.x = -1 causes some issues.

I ended up implementing TwistedTwigleg idea. I added one more Collision Shape to my character and used getnode() to disable / enable what I needed:

func _physics_process(delta): if Input.is_action_pressed("p_right"): velocity.x = SPEED $AnimatedSprite.play("walk") $AnimatedSprite.flip_h = false get_node("CollisionShape2D_h_left").disabled = true get_node("CollisionShape2D_h_right").disabled = false elif Input.is_action_pressed("p_left"): velocity.x = -SPEED $AnimatedSprite.play("walk") $AnimatedSprite.flip_h = true get_node("CollisionShape2D_h_left").disabled = false get_node("CollisionShape2D_h_right").disabled = true else: velocity.x = 0 if on_ground == true: $AnimatedSprite.play("idle")

I've learned something nice today!

2 years later