Raycast2D not functioning?

slamjam29slamjam29 Posts: 19Member

So I've been trying to follow Game Endeavor's video on how to build a player character, as shown here:

But the issue is that the Raycast2D's aren't functioning at all, and since the Raycasts are vital to determining whether the player's on the ground or not in the tutorial, not only do I end up being able to jump infinitely, but the player character is stuck in the jump animation. Here's my current code, which isn't much different from the video:

    extends KinematicBody2D

    const UP = Vector2(0, -1)
    const SLOPE_STOP = 64

    var velocity = Vector2()
    var move_speed = 5 * 96
    var gravity = 800
    var jump_velocity = -700
    var is_grounded

    onready var raycasts = $Raycasts
    onready var anim_player = $Body/AnimationPlayer

    func _physics_process(delta):
        _get_input()
        velocity.y += gravity * delta
        velocity = move_and_slide(velocity, UP, SLOPE_STOP)

        is_grounded = _check_is_grounded()

        _assign_animation()

    func _input(event):
        if event.is_action_pressed("lt_up") && is_grounded:
            velocity.y = jump_velocity

    func _get_input():
        var move_direction = -int(Input.is_action_pressed("lt_left")) + int(Input.is_action_pressed("lt_right"))
        velocity.x = lerp(velocity.x, move_speed * move_direction, 0.2)
        if move_direction != 0:
            $Body.scale.x = move_direction

    func _check_is_grounded():
        for raycast in raycasts.get_children():
            if raycast.is_colliding():
                return true

        return false

    func _assign_animation():
        var anim = "Idle"

        if is_grounded:
            anim = "Jump"
        elif velocity.x != 0:
            anim = "Run"

        if anim_player.assigned_animation != anim:
            anim_player.play(anim)

Is there anything I'm doing wrong here? And yes, I made sure that the Raycasts were enabled beforehand.


Tags :

Comments

  • MegalomaniakMegalomaniak Posts: 3,826Admin
    edited July 22

    That tutorial id is for an older version of godot. I recommend you look up a newer tutorial, perhaps one on move_and_slide_with_snap()

  • fire7sidefire7side Posts: 131Member

    It's probably your set up and getting the raycast. If raycast is a child of body, say, then you would need:
    $body/Raycasts. Are you getting any errors in the debug?

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file