I want to make my sprite change direction but...

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

I’m new to Godot, and I’ve been having a problem recently: every time I use a node to change my sprite’s direction using “A” and “D” (it’s a platform game), the sprite stretches. Below are the codes I used:

func _get_input():
var move_direction = int(Input.is_action_pressed("move_right")) - 	int(Input.is_action_pressed("move_left"))
velocity.x = move_speed * move_direction

if move_direction !=0:
	$texture.scale.x = move_direction

some1 help me

In the posted code, I assume $texture is a reference to the sprite node itself? If that’s the case, I don’t really see anything wrong with the code. What does your scene tree structure look like? Which node is the above script attached to? Are there any ancestor nodes that have non-standard scaling values assigned to them? Is there more related code here?

jgodfrey | 2022-12-08 23:10

Yes! The “$texture” is my sprite node. Here is the all structure:

extends KinematicBody2D

var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720 / 2

func _physics_process(delta: float) -> void:
velocity.y += gravity * delta

_get_input()

if Input.is_action_pressed("jump"):
	velocity.y = jump_force


velocity = move_and_slide(velocity)

func _get_input():
var move_direction = int(Input.is_action_pressed("move_right")) - 				int(Input.is_action_pressed("move_left"))
velocity.x = move_speed * move_direction

Blackyy | 2022-12-09 01:56

:bust_in_silhouette: Reply From: Gluon

Try making the code a little more explicit and see if you still get the same error, try this for instance:

func _get_input():
    var move_direction = vector2d
    if Input.is_action_pressed("move_right"):
        move_direction.x = 1
    elif Input.is_action_pressed("move_left"):
         move_direction.x = -1
    velocity.x = move_speed * move_direction