So basically I was trying to use the default script for 2d player controller and now I’m struggling to make a different animation based on the direction variable in the script and my error code says that I need two parameters for input.get_axis() in my if statement where only
put one so that when I go left the sprite faces left but its not working.
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
func _physics_process(delta: float) -> void:
$AnimatedSprite2D
var direction := Input.get_axis("ui_left", "ui_right")
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
if direction == Input.get_axis("ui_left"):
set_animation("default")
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
I’ve tried looking it up but I can’t find anything. so plz help
Liam Briley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Have a look at the documentation of Input.getAxis : Docs
The function always needs two parameters. One for negative and one for positive. The result is a float between -1 and 1 which represents how far the axis is moved in one of the specific directions.
for your direction variable this means: If the user fully moves the stick to the left, direction will be -1, since you declared this your negative action. And full right would be 1.
In conclusion, if you want to check, if the player moves to the left your comparison should look like this:
if direction < 0:
I am going to assume that you only have animations for left and right, as this seems like the start of a platformer game.
With Input.get_axis()
, a float is stored in your variable direction
. If you go left, direction
is set to -1
, and if you go right, the direction
is set to 1
. If you don’t press anything, the direction
is set to 0
.
So, if you want to change the animation based on direction, you can check if direction < 0
for left, or if direction > 0
for right. You’ll additionally want to do this outside of checking for a jump if you want animations where the character is running/idle.
Now just some other points to help you on your way.
You are referencing $AnimatedSprite2D
in your _physics_process
, but you’re not storing it anywhere, so you can’t actually use it. You’ll want to have something like this:
var animated_sprite = $AnimatedSprite2D
.
Because of how things are loaded in Godot, you’ll also need to move this outside of the _physics_process
and into the main body of the script. If you don’t, your script will try to load the AnimatedSprite2D
every physics frame (60 times per second). So at the top where you have your constants, you’ll need to add the @onready tag:
@onready var animated_sprite = $AnimatedSprite2D.
This will allow you to change your animations using animated_sprite.play(<animation_name>)
.
Now, with the gravity, you are currently adding it to the velocity directly, and not the velocity’s y
component. Velocity uses a Vector2
which has an x
and a y
component. You can think of anything that goes from left to right as the x
part, and anything that goes up or down as the y
part.
So, here I would use velocity.y += get_gravity() * delta
to make gravity work up and down.