Movement programming

feketerigofeketerigo Posts: 2Member
edited January 23 in Programming

Hi, rookie's issue's here. I recently started follow tutorial for 'your first game' (http://docs.godotengine.org/en/3.0/getting_started/step_by_step/your_first_game.html ) the problem accured out of nowhere, cus I strictly followed to what described.

    extends Area2D

    export (int) var speed
    var screensize

    func _ready():
    screensize = get_viewport_rect().size

    func _process(delta):
        var velocity = Vector2()
        if Input.is_action_pressed("ui_right"):
            velocity.x += 1
        if Input.is_action_pressed("ui_left"):
            velocity.x -= 1
        if Input.is_action_pressed("ui_down"):
            velocity.y += 1
        if Input.is_action_pressed("ui_up"):
            velocity.y -= 1
        if velocity.length() > 0:
            velocity = velocity.normalized() * speed
            $AnimatedSprite.play()
        else:
            $AnimatedSprite.stop()

        position += velocity * delta
        position.x = clamp(position.x, 0, screensize.x)
        position.x = clamp(position.y, 0, screensize.y)

So, this is the code. What is wrong with it, because when game is executed character do not move as expected? When I press ui_left or ui_right nothing happens, when I press UP or DOWN characer starts to move up and down, but verticaly(as if I pressed up+left).

Answers

  • MegalomaniakMegalomaniak Posts: 1,087Admin
    Answer ✓

    Last line:
    position.x = clamp(position.y, 0, screensize.y)

    should perhaps be:
    position.y = clamp(position.y, 0, screensize.y)

    no?

  • feketerigofeketerigo Posts: 2Member

    Thank you, @Megalomaniak! I can't believe that I didn't notice that staring at the code a whole hour

  • MegalomaniakMegalomaniak Posts: 1,087Admin

    That happens to all of us from time to time. ;)

Sign In or Register to comment.