Basic player plateformer code but cant use directional based animations in godot

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

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật