I am new to godot and I am trying to flip the character whenever it is going left or right, I tried to use flip_h but then the collision shape would no more fit the sprite, instead I am changing the scale.x of the character, but when it’s moving left, the scale.y of the character is changing even if I am not touching it. I unlinked the Scale property via editor but the problem is still there.
extends CharacterBody2D
const SPEED = 400.0
const JUMP_VELOCITY = -900.0
@onready var animated_sprite_2d = $AnimatedSprite2D
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
var move_right = int(Input.is_action_pressed("right"))
var move_left = int(Input.is_action_pressed("left"))
if move_right > move_left:
scale.x = 1
print("MOVING RIGHT", "\nScale X", scale.x, "\nScale Y",scale.y)
elif move_left > move_right:
scale.x = -1
print("MOVING LEFT", "\nScale X",scale.x, "\nScale Y",scale.y)
# Determine animation
if (velocity.x > 1 || velocity.x < -1) and is_on_floor():
animated_sprite_2d.animation = "running"
else:
animated_sprite_2d.animation = "idle"
# Apply gravity
if not is_on_floor():
velocity.y += gravity * delta
animated_sprite_2d.animation = "jumping"
if velocity.y >= 0:
animated_sprite_2d.animation = "falling"
# Apply jump
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Determine speed based on direction
if move_right or move_left:
velocity.x = (move_right - move_left) * SPEED
else:
velocity.x = 0
move_and_slide()
I created a script in the AnimatedSprite2D and now all the animations are managed here, but the problem with the scale.y is still there. Maybe I am just stupid sorry, but could you explain more?
Here is the script for animations:
extends AnimatedSprite2D
onready var character_body = get_parent()
var is_running = false
func _physics_process(delta):
# Determine animation
if character_body.velocity.x > 1 or character_body.velocity.x < -1:
if character_body.is_on_floor():
animation = "running"
is_running = true
else:
is_running = false
else:
animation = "idle"
is_running = false
if character_body.is_jumping:
animation = "jumping"
elif character_body.is_falling:
animation = "falling"
Here the script for the character movement:
extends CharacterBody2D
const SPEED = 400.0
const JUMP_VELOCITY = -900.0
var move_right = int(false)
var move_left = int(false)
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var is_jumping = false
var is_falling = false
func _physics_process(delta):
move_right = int(Input.is_action_pressed("right"))
move_left = int(Input.is_action_pressed("left"))
if move_right > move_left:
scale.x = 1
print("MOVING RIGHT", "\nScale X", scale.x, "\nScale Y",scale.y)
elif move_left > move_right:
scale.x = -1
print("MOVING LEFT", "\nScale X",scale.x, "\nScale Y",scale.y)
# Apply gravity
if not is_on_floor():
velocity.y += gravity * delta
is_jumping = true
if velocity.y >= 0:
is_falling = true
else:
is_jumping = false
is_falling = false
# Apply jump
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Determine speed based on direction
if move_right or move_left:
velocity.x = (move_right - move_left) * SPEED
else:
velocity.x = 0
move_and_slide()