This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi everyone! :)

I am a total newbie to GoDot Engine, never done any coding before and never tried to make my own game before. But I thought I'd give it a try, see how it goes.

I've watched a bunch of videos on the YouTube channel GD Quest. Watched and followed this video "Code Your First Complete 2D Game with Godot".

However I have problems. I have followed the code exactly, checked everything. I'm lost.

Now the problem is the movement, the W and S key aren't doing what they should. My player moves diagonally up and down towards the corners, that's it. Also somehow it can leave the game area at the bottom. It also flips upwards and downwards when I press the W and S keys. But I just can't get it to walk in a straight line, up, down, left and right.

So this is my code. Note I kinda stopped around timestamp 13:00 ish, because it wasn't working for me.
Maybe this is considered old code now and doesn't work, I don't know. It seems to work for others, but I don't know what I've missed.

I read somewhere that vector can cause problems, but ehh yah I don't know anything about that. I just followed the tutorial.

I hope someone can shed some light on this. :) Any help is much appreciated!

extends Area2D

export var speed = 400.0
var screen_size = Vector2.ZERO

func ready():
size = getviewportrect().size

func process(delta):
var direction = Vector2.ZERO
direction.x += 1
if Input.isactionpressed("move_left"):
direction.x -= 1

if Input.is_action_pressed("move_down"):
    direction.y += 1
if Input.is_action_pressed("move_up"):
    direction.y -= 1

if direction.length() > 0:
    direction = direction.normalized()

position += direction * speed * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.x, 0, screen_size.y)

if direction.x != 0:
    $AnimatedSprite.animation = "right"
    $AnimatedSprite.flip_h = direction.x < 0
    $AnimatedSprite.flip_v = false
elif direction.y != 0:
    $AnimatedSprite.animation = "up"
    $AnimatedSprite.flip_v =direction.y > 0
Godot version V3.4.2
in Engine by (20 points)

1 Answer

+1 vote
Best answer
position.y = clamp(position.x, 0, screen_size.y)

This line appears to be the issue. It should be:

position.y = clamp(position.y, 0, screen_size.y)
by (8,580 points)
selected by

Ahh now I feel so silly, one little letter. Well one incorrect letter can make a huge difference when it comes to coding.

Thanks a lot! :D

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.