All Questions

Community

P
Lalin Paranawithana

Character doesn't move!

The character doesn't respond to any input from the player. I've added the actions to the Input Map and made the modifications to Player.gd. When I tried moving 'velocity = move_and_slide(velocity)' to Player.gd, the character stopped falling as well. Changing the y value of direction to 1.0 had no effect on it.


  • Nathan Lovato replied

    Could you share your whole code? Both Actor.gd and Player.gd.

    You can make code blocks here by clicking the "magic wand" icon in the top-left and choosing "Code".

    It looks like this when you do.

    I'll need to see your code to help you.

  • P
    Lalin Paranawithana replied

    Thanks for replying. Here is the code for Actor.gd:

    extends KinematicBody2D
    class_name Actor

    export var speed: = Vector2(300.0, 1000.0)
    export var gravity: = 3000.0
    var velocity: = Vector2.ZERO

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

    This is the code for Player.gd:

    extends Actor

    func _physics_process(delta: float) -> void:
    var direction: = Vector2(
    (Input.get_action_strength("move_right") -
    Input.get_action_strength("move_left")),
    1.0
    )
    velocity = speed * direction

    I moved 'velocity = move_and_slide(velocity)' back to Actor.gd to fix the issue of the character not falling onto the floor.


  • Nathan Lovato replied

    The code should work with the following line moved to Player.gd.

      velocity = move_and_slide(velocity)

    Could you not move the player horizontally either, on top of it not falling? If so, did you drag and drop the new Player.gd script on the Player node? If you don't do so, it'll still have the code of Actor.gd running only, causing it not to move at all.

    Keeping the call to move_and_slide() on the parent Actor class doesn't solve the issue, or rather, it causes another one which is that movement will happen with a one-frame delay, making your game feel less good than it could.

  • Nathan Lovato replied

    I'll let you check your project and tell me if the above helps. If not, could you upload your godot project as a .zip archive to a website of your choice (dropbox, wetransfer...) and post it here?

    Note I'll be absent from later this afternoon to Thursday.

  • P
    Lalin Paranawithana replied

    That's right. I can't move it horizontally either. The only reason I moved that line back to Actor.gd is because it stops falling altogether otherwise. I am also getting a weird black screen when I run the game by pressing F6.

    You can download my project files from here: https://send.tresorit.com/a#UAj42OBZEqBIbAOopbIcaQ.

  • P
    Lalin Paranawithana replied

    By the way, I just wanted to let you know that there is a typo in the automated email I got when I created this topic. It says that you will be on vacation 'until Thursday 19'. 

  • Nathan Lovato replied

    I just tried your project, opened LevelTemplate, moved the line from Actor.gd to Player.gd, and everything works. Here's the code:

    Player.gd

    extends Actor

    func _physics_process(delta: float) -> void:
    var direction: = Vector2(
    (Input.get_action_strength("move_right") -
    Input.get_action_strength("move_left")),
    1.0
    )
    velocity = speed * direction
    velocity = move_and_slide(velocity)

    And Actor.gd

    extends KinematicBody2D
    class_name Actor

    export var speed: = Vector2(300.0, 1000.0)
    export var gravity: = 3000.0
    var velocity: = Vector2.ZERO

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

    Note that you had spaces for indentation in one file and tabs in another. You'll want to be consistent with that, because while you can have either in one file, when you copy and paste code and they get mixed, you'll get compiler errors.

  • Nathan Lovato replied

    And thanks for mentioning the date on the auto-response email, I'll fix it before leaving!


  • P
    Lalin Paranawithana replied

    Thanks! It got fixed. You saved me from having to start over from scratch. Enjoy your vacation! :-D

  • Nathan Lovato replied

    My pleasure, I hope you'll enjoy the course!