0 votes


I'm a web developer which wants to learn more about Godot but I'm struggling with my initial project. Basically I want to move an Area2D only on x-y axis and create a Line2D to simulate the movement.

In order to do that I've create a Player Node (Area2D) with the following script attached:

extends Area2D

export var speed = 500
var target = Vector2()
var screen_size

func _ready():
    screen_size = get_parent().get_viewport_rect().size

func _input(event):
    if event is InputEventScreenTouch:
        target = event.position

func _process(delta):
    var velocity = Vector2()
    if position.distance_to(target) > 10:
        velocity = target - position

    if velocity.length() > 0:
        velocity = velocity.normalized() * speed

    position.x += velocity.x * delta
    if round(velocity.x) == 0:
        position.y += velocity.y * delta

    position.x = clamp(position.x, 0, screen_size.x)
    position.y = clamp(position.y, 0, screen_size.y)

Also I've attached a Line2D node with a script attached:

extends Line2D

func _process(delta):
    global_position = Vector2()
    if points.size() > 0:
        add_point(Vector2(points[-1].x, get_parent().global_position.y))

The problem is the moment doesn't have the same speed and also sometimes the Line2D stops drawing. I created a video to show what's happening: YouTube Demo

I would be grateful if somebody can help me.
Thanks in advance

in Engine by (12 points)

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.