I’m making a 2D side scrolling RPG, and the Player character node has a Weapon node and animation player as children.
I’m trying to use scale.x = 1 or scale.x = -1 to flip the character and all child nodes based on direction, yet it seems to be flipping every frame when setting scale.x = -1. I’m trying to do this so I don’t have to recreate the animations the other side in the animation player.
My function below for getting the input and direction is called from within the _physics_process function.
The below is my function for getting the input and setting the direction:
func get_input_and_direction() → void:
# Get the input direction: -1, 0, 1
var direction: float = Input.get_axis(“move_left”, “move_right”)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 7.0)
print(scale.x) **# Prints 1 every loop**
# Flip the sprite based on input
if direction > 0:
scale.x = 1
elif direction < 0:
if scale.x != -1:
print("Before set " + str(scale.x)) **# Prints 1**
scale.x = -1
print("After set " + str(scale.x)) **# Prints -1**
I’d greatly appreciate if someone shine some light on to why this is happening, I can’t figure it out.
I’m also open if you’ve got suggestions on how I can write my code better or handle flipping the Player node.
It does when assigning because of how transforms are done, it’s complicated mathematics but it’s put simply that there’s no way to distinguish negative x scale from other things because of how 2D transforms are computed, it has to be combined with the transform or parents etc.
@athousandships what if you need to flip the player by it’s root node, where you cant flip by flip_h because it doesn’t flip collisions. Scale.x -1 is used for flipping an object, collisions and all. and if that doesnt work, how to flip by root node?
You need to flip the individual parts, using what suits the situation, moving or rotating, you can have a shape that doesn’t need to be flipped for example, or switch shape being active when facing different directions, it all depends on your situation
Sorry to necro this Topic, but I saw multiple of these without (most of them) mentioning how to solve this with Transforms, so I made a guide on this very thing!
Tl;dr, you can achieve stable flipping in the x direction with something like the following on the root node:
Hey, the reason your scale.x keeps resetting is likely because setting just one component of the scale vector (scale.x = -1) sometimes doesn’t persist properly in Godot. Instead, you should assign the whole scale vector at once, like this:
scale = Vector2(-1, 1)
Also, check if your AnimationPlayer is animating the scale property — if it is, it will override your code every frame and reset scale.x back to 1. Removing or disabling those scale keyframes should fix the issue.
Alternatively, if you are using a Sprite2D or AnimatedSprite2D node for the character’s visuals, flipping it using the flip_h property is usually cleaner:
$Sprite2D.flip_h = direction < 0
This way, you avoid messing with the whole node’s scale and potential side effects.
Finally, for a cleaner code, you can do:
if direction != 0:
scale = Vector2(sign(direction), 1)
I just tested what you said @esp8266 , and it seems like (at least in my case) that the node still gets “confused” when doing:
scale = Vector2(-1, 1)
In my case it would flip the image upside down sporadically.
I think using the “helper properties” scale, rotation, and skew to indirectly set the transform isn’t ideal in a few edge cases (such as negative scale.x). But setting the transform directly seems to always behave in a stable manner.
Hey all - I just wanted to share my solution in case it helps someone…
My solution was to put all of the visual (sprite) nodes into their own Node2D and update the scale.x of the new visuals-only parent node when the character flips from left to right.
Here’s what I did:
First I created a Node2D called “Visuals” as a child of my root node (fyi it’s a CharacterBody2D)
Next, I moved all of the Sprite2D nodes into the new “Visuals” node
Doing this avoids messing with any physics calculations that a physics body node may be doing when velocity changes, and as a bonus the scene structure is a bit more organized.