Topic was automatically imported from the old Question2Answer platform.
Asked By
Bleenx
Old Version
Published before Godot 3 was released.
I’ve tried to use the set_flip_h in my script, but it says it’s nonexistent, so I guess I need to call the function somewhere, or create it? I’m also guessing in order to use that I need to grab the sprite node that’s a child of the Rigidbody2D?
I’ve also tried to just set the scale to -1 when going left, but it makes the character erratic and scale back and forth. Does anyone have a good way of flipping the character on the horizontal axis? Would creating a custom function to scale the character be the best way and then call it when I need it?
var Direction = "Left"
func ChangeDirection():
if Direction == "Left":
get_node( "Sprite" ).set_flip_h( false )
Direction = "Right"
elif Direction == "Right":
get_node( "Sprite" ).set_flip_h( true )
Direction = "Left"
so, in any part of the program, call ChangeDirection, change the sprite of the object. You can improve this to have arguments and other parameters if you wish
This is my movement function:
func Movement():
var leftButton = Input.is_action_pressed("leftButton")
var rightButton = Input.is_action_pressed("rightButton")
if rightButton:
set_axis_velocity(Vector2(moveSpeed, 0))
elif leftButton:
set_axis_velocity(Vector2(-moveSpeed, 0))
else:
set_axis_velocity(Vector2(0, 0))
Where would I call the ChangeDirection function? I can’t seem to get it to flip no matter what I do. Should I rewrite my movement code?
Bleenx | 2016-05-13 17:33
ok, add the line:
if rightButton:
get_node( "Sprite" ).set_flip_h( false ) #this line
...
remember to change the path for the sprite and if the direction is wrong change false to true.
than tell me if works
tiernich | 2016-05-13 17:54
That worked! I was trying to add an “&” operator for some silly reason. My final movement code looked like this:
# Player Movement Control (left and right)---------------------|
func Movement():
var leftButton = Input.is_action_pressed("leftButton")
var rightButton = Input.is_action_pressed("rightButton")
if rightButton:
set_axis_velocity(Vector2(moveSpeed, 0))
get_node( "playerSprite" ).set_flip_h( false )
elif leftButton:
set_axis_velocity(Vector2(-moveSpeed, 0))
get_node( "playerSprite" ).set_flip_h( true)
else:
set_axis_velocity(Vector2(0, 0))
Thanks a lot for your help. I had the right idea but was doing it completely wrong.
I want to provide an answer for Godot 3 users who are stumbling upon this page in 2018, like I did:
You can accomplish flipping an entire Node2d and all its children through use of apply_scale.
Here is how I have horizontal flipping set up for my platform game:
const DIRECTION_RIGHT = 1
const DIRECTION_LEFT = -1
var direction = Vector2(DIRECTION_RIGHT, 1)
func set_direction(hor_direction):
if hor_direction == 0:
hor_direction = DIRECTION_RIGHT # default to right if param is 0
var hor_dir_mod = hor_direction / abs(hor_direction) # get unit direction
apply_scale(Vector2(hor_dir_mod * direction.x, 1)) # flip
direction = Vector2(hor_dir_mod, direction.y) # update direction
Then, when needed, just call:
set_direction(DIRECTION_LEFT)
or:
set_direction(DIRECTION_RIGHT)
The Node2d will then flip if it is facing the opposite direction of the parameter.
The nice thing about this implementation is that the var direction can be applied to dependent scenes, such as when determining the direction of a projectile:
func create_projectile():
var projectile = PROJECTILE_SCENE.instance()
projectile.direction = direction
get_parent().add_child(projectile)
Note the above function does not define PROJECTILE_SCENE or cover translating it to the correct coordinates - it is only to show the use of direction.
Finding this question years later when wondering the same thing myself, I found the following useful macros under part of Transform2D:
FLIP_X = Transform2D( -1, 0, 0, 1, 0, 0 )
Transform2D with mirroring applied parallel to the X axis.
FLIP_Y = Transform2D( 1, 0, 0, -1, 0, 0 )
Transform2D with mirroring applied parallel to the Y axis.
These can be use to flip an object and all its children in a way similar to the one Rob describes in his answer, but in the following single line:
transform *= Transform2D.FLIP_X
and the funny thing about @Rob Blob is that the only code working is :
apply_scale(Vector2(-1, 1))
and all the other stuff are excessive!
javadmajidi | 2020-11-02 17:44
A disclaimer: be careful when trying to change your character’s rotation after modifying its transform. The rotation value is adjusted based on the transform at each processing step.
Sometimes simple is best, just stick to set_flip_h(), I’ve been using scale.x * -1 in order to reflect the sprite but this approach modifies de value of rotation_degrees and makes everything more complicated.
I found this out the hard way… My rotation was all screwy and had no idea why and after hours of debugging it hit me. Yes just use set_flip_h(). Less headache down the line.