+1 vote

Hi there.
I wanna my player to scale to (scale.x = 1) when i press the right key and -1 when i press left.
I have the following code:

if Input.is_action_just_pressed("ui_left"):
    scale.x = -1
if Input.is_action_just_pressed("ui_right"):
    scale.x = 1

But every time that i push left the scale alter between 1 and -1.
And when i push right nothing happen.

Godot version 3.2.3
in Engine by (68 points)

3 Answers

+1 vote

It sounds like you've mapped the left arrow key to both the ui_left and ui_right actions. Check your key mappings and I think you'll see the problem.

by (10,856 points)

I did check and them aren't.

Errrr... Are you sure? That would cause exactly the problem you mention, and I can't think of anything else that would cause it... The code you posted looks fine.

Im pretty sure, man.

Is the full project available some place where it can be looked at?

I am sure too

+1 vote

with this code you are flipping the entire player, not only the sprite, meaning left and right get reversed, and all the rest of your player code will loose sense.
change it to to flip the sprite only

if Input.is_action_pressed("ui_left"):
    $NeoPlayer.scale.x = -1
elif Input.is_action_pressed("ui_right"):
    $NeoPlayer.scale.x = 1  

also, why did you created your own INPUT() function inside process, instead of using the _input(event) function that fires every time an input is received

by (1,213 points)

I actually already do that, but thanks anyway.
Isn't the method that i prefer, cuz i also wanna to flip the colliders though it, instead of using code, but them can not be a child of the Node2D node, but that is better that nothing.
Also, the input(event) wasn't working pretty fine for me, so i do it myself.

well you can flip the spirte AND the collider separately, it's just one additional line of code.
or you can place the sprite as a child of the collider and flip the collider to flip both

0 votes

If I were you and this happened, I would most likely create a variable which would control them. Try this.

var scaleup = false
var scaledown = false


func _input(event):
    if scaleup == false:
        if Input.is_action_just_pressed("ui_left"):
            scale.x = -1
            scaledown = true
            scaleup = false
    if scaledown == false:
        if Input.is_action_just_pressed("ui_right"):
            scale.x = 1
            scaleup = true
            scaledown = false

if this doesn't work in the input(event) either, then try again in the process.

by (276 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.