My character only scales one way.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Youbadass

I think i already asked this but i couldn’t fint the thread so sorry if im spamming.

My character scales only one way. I have it so that he scales right when he goes right (-1) and scales left when he goes left (1). But for some weird reason when he is turned right and i press left he doesn’t turn left. Idk why that is but I think when he changes scale to -1 that scale is now 1 so when he is supposed to turn left he doesn’t, and when he is supposed to sill face right he then turns left. Idk how to explain it better.

Here is the script:
extends KinematicBody2D

var velocity = Vector2(0,0)
const SPEED = 180
var jumpforce = -800
const gravity = 30

func _physics_process(_delta):

if Input.is_action_pressed("right"):
	velocity.x = SPEED
	
if Input.is_action_pressed("left"):
	velocity.x = -SPEED
	
velocity.y = velocity.y + gravity

if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = jumpforce
if Input.is_action_just_pressed("atak 2"):
	$Sprite/atak.play("atak dzida")
velocity = move_and_slide(velocity,Vector2.UP)

velocity.x = lerp(velocity.x,0,0.5)

if Input.is_action_just_pressed("atak"):
	$Sprite/atak.play("atak")
if Input.is_action_just_pressed("left"):
	scale.x = abs(1)
if Input.is_action_just_pressed("right"):
	scale.x = -1

What does your sprite look like? Is it facing left by default?

exuin | 2021-03-08 15:25

My sprite is just a dude. He is facing left by default. On the inspector its scale is x:1 y:1.

Youbadass | 2021-03-08 16:33

:bust_in_silhouette: Reply From: Kurotsuki

I dont know what the abs() function does because i’ve never used it before but i’ve made a 2D platformer before and i just set scale.x as either 1 or -1 depending on keyboard input and it works without any bugs, i honestly dont see the cause of your problem either and if the bug remains i apologize for not being much help

abs() is absolute value and doesn’t seem to have any use in the function.

exuin | 2021-03-09 04:48

Do you have the scale change under _func physics process or some other function? Because I also did it under _func _unhandled_input and it worked the exact same way.

Youbadass | 2021-03-09 12:23

I divide my code into functions so anything input related i put in a function i named “inputs” and called it under _physics process, i set it so that if the a key is pressed it will set scale.x into 1 repeatedly so that it cant change into any other value since its being set every frame if the a key is pressed, same goes for d key

Kurotsuki | 2021-03-09 14:08

Could you copy paste the line of code here or if you were following a tutorial could you give me a link? Because when i put it under _physics_process it just keeps jitterng when i hold d but when i hold a it doesn’t do anything.
My only guess is its multiplying the value of the scale, but i didn’t ever tell it to do that.

Youbadass | 2021-03-09 14:15

I’ve deleted the project way back then but essentially it went aomething like this

func UserInput():
    if Input.is_action_pressed("left"):
        scale.x = 1
    elif Input.is_action_pressed("right"):
        scale.x = -1

func _physics_process(delta):
    UserInput()

If the bug remains then i dont even know anymore

Edit: wait wait wait maybe its got something to do with the fact that you’re using is_action_just_pressed, that line will return true once and will only return false until the key is lifted and pressed again but hey i dont know im on my phone cant really test it

Kurotsuki | 2021-03-09 16:17

:bust_in_silhouette: Reply From: Hyper Sonic

See my answer on https://forum.godotengine.org/92282/why-my-character-scale-keep-changing?show=146969#a146969

To sum up, when you go right, scale.x = -1 is converted into scale.y = -1 and rotation_degrees = 180, but scale.x remains 1 internally.

So if you then go left, scale.x = 1 does nothing (instead of flipping back).
If you go right again, scale.x = -1, and it will incorrectly flip again.