Collision Shape not flipping

Godot Version

4.5

Question

I’m working on a 2d platform and the issue I’m facing now is whenever the character is facing the right direction the collision shapes are in the correct spots lined up on the character until they turn to the left and the shapes are no longer on the character and still face the initial direction. How would I fix this?

extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

const SPEED = 975.0
@export var jump_velocity : float = -1300.0

var isAttacking: bool = false; 
var AttackPoints = 3;

func _physics_process(delta: float) -> void:
	if isAttacking:
		return

	# add animation
	if velocity.x > 1 or velocity.x <-1:
				$AnimatedSprite2D.play("Run")
	else:
		animated_sprite_2d.animation = "Idle"
		
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
		$AnimatedSprite2D.play("Jump")
		

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity

	# Get the input direction and handle the movement/deceleration.
	var direction := Input.get_axis("left", "right") 
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	
	
	#attack
	if Input.is_action_just_pressed("attack") and not isAttacking and is_on_floor() && AttackPoints == 3:
		$AttackResetTimer.start();
		$AnimatedSprite2D.play("Attack_1")
		isAttacking = true;
		$AttackArea/CollisionShape2D.disabled = false; 
		AttackPoints = AttackPoints - 1;
	elif Input.is_action_pressed("attack")  and not isAttacking and is_on_floor() && AttackPoints == 2:
		$AttackResetTimer.start();
		$AnimatedSprite2D.play("Attack_2");
		isAttacking = true;
		$AttackArea/CollisionShape2D.disabled = false; 
		AttackPoints = AttackPoints - 1;
	elif Input.is_action_pressed("attack")  and not isAttacking and is_on_floor() && AttackPoints == 1:
		$AttackResetTimer.start();
		$AnimatedSprite2D.play("Attack_3");
		isAttacking = true;
		$AttackArea/CollisionShape2D.disabled = false; 
		AttackPoints = AttackPoints - 1;
	if Input.is_action_just_pressed("attack") and not isAttacking and  not is_on_floor():
		$AttackResetTimer.start();
		$AnimatedSprite2D.play("Jump Attack");
		isAttacking = true;
		$AttackArea/CollisionShape2D.disabled = false; 
	
	#Direction
	if direction == 1.0:
		animated_sprite_2d.flip_h = false
	elif direction == -1.0:
		animated_sprite_2d.flip_h = true
		
	
		
		if isAttacking:
			velocity = Vector2.ZERO


func _on_animated_sprite_2d_animation_finished() -> void:
	if isAttacking:
		isAttacking = false;
	


func _on_attack_reset_timer_timeout() -> void:
	AttackPoints = 3;

Move them to correct positions whenever the movement direction changes.

Where and what would I type if you don’t mind me asking

Store the positions for each orientation in some custom properties in your script. Whenever the flip happens, assign the corresponding positions to collision shape nodes. To determine if flip happened, store direction value from the previous frame and compare it to the current direction value.

I would avoid float comparisons like that. They might fail at times. That might be adding to issue.

What would be the alternative?

Check out this post. Look for the word epsilon: comparing floats - #2 by system . It helps get a consistent float comparison.

There was a similar topic not too long ago, where it was answered here:

Maybe you can try the same solution.

1 Like