Topic was automatically imported from the old Question2Answer platform.
Asked By
cyrus8088
Old Version
Published before Godot 3 was released.
I looked around for a question similar to mine, i just couldn’t find it, so i’m making my own post
(Also i’m relatively new to coding so this is a fairly simple script)
extends Area2D
export (int) var SPEED
var velocity = Vector2()
var screensize
var position = 1
var input = 1
func _ready():
screensize = get_viewport_rect().size
func _process(delta):
_ready(set_process_input(true))
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
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)
That is my code for the 2D Character’s movement on a flat surface.
No matter how many things i change, i just can’t get it to move no matter what input i press, please help me.
Are you setting the SPEED variable in the editor to a value?
It seems that getting viewport size at ready funtion, was returning a 0, 0 vector.
This way it updates every frame. Not sure if it’s useful for you, but one good thing is that you can change the size of the screen during the game.
another issue at least form me, was that I needed to declare “screensize” as Vector2 in order to get the size. Anyway it returned 0, 0.