0 votes

My game is a platformer but when I hit the right arrow key or left ones , the player doesn't move as expected (although it jumps well). Also animation stops. I have tried to do a lot of things , even make a new project from scratch (although I copied and pasted the player script) . I think problem is with player script but I couldn't find one :

The script:

extends KinematicBody2D

var velocity = Vector2()
var speed = 200
var max_speed = 100
const GRAVITY = 10

const FLOOR = Vector2(0, -1)
var on_ground = false

func _ready():

func _physics_process(_delta):

    if velocity.x == 0:
        $AnimatedSprite.speed_scale = 1
    if on_ground == false:
        $AnimatedSprite.speed_scale = 2

    if Input.is_action_pressed("ui_right"):
        #if on_ground == true:
        velocity.x = speed

        $AnimatedSprite.flip_h = false
        $AnimatedSprite.speed_scale = 2
    elif Input.is_action_pressed("ui_left"):
        #if on_ground == true:
        velocity.x = -speed

        $AnimatedSprite.flip_h = true
        $AnimatedSprite.speed_scale = 2

    if Input.is_action_just_pressed("damage"):
        $AnimatedSprite.speed_scale = 2

        velocity.x = 0

    if Input.is_action_just_pressed("ui_up"):
        if on_ground == true:
            velocity.y = -300
            on_ground = false

    velocity.y += GRAVITY
    if is_on_floor():
        on_ground = true
        on_ground = false

    velocity = move_and_slide(velocity, FLOOR)

maybe I am missing out something, pls help.

If you need project files, I 'd post that too.

in Engine by (44 points)

1 Answer

+1 vote
Best answer

The problem is in this block:

 if Input.is_action_just_pressed("damage"):
        $AnimatedSprite.speed_scale = 2

        velocity.x = 0 

If "damage" is not just pressed (which will be the case almost all the time since the player wont be damaging stuff constantly throughout the game), velocity along the x axis will be reset to zero even when "uiright" and "uileft" is pressed.

I actually cant seem to find the reason for you to reset the velocity at that point.
Try declaring velocity as an empty vector2 at the start of physicsprocess then set idle animation to be playing if the player is not moving or damaging stuff.

by (2,001 points)
selected by

Thanks I thought that i have done something wrong but was not able to see that. Thanks anyways

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.