# How to move in one direction every pass

Attention Topic was automatically imported from the old Question2Answer platform.

Hello all…
What I want is a code in (C # or GDscript) to make the player move the same way as the yellow ball … Watch the video (https://youtu.be/swWtPulkuMw)
more clarification:
I want the player to move every time (dragging the phone screen or with the keyboard buttons) a continuous movement towards dragging or scrolling (up or down right or left) until it hits a wall or any other obstacle so I can move it again … It is a puzzle game
thank you all .

the movement could be done using acceleration and deceleration . the continuous movement , you could restrict accepting Input to the condition “if velocity.length() == 0:” accept Input . an area or RayCast could be used to signal the ball to decelerate to a stop shy of the wall.

ArthurER | 2020-10-04 06:14

like this:

``````func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if velocity.length() == 0:
velocity = velocity.normalized() * speed
``````

r1diq | 2020-10-04 07:57

shy on time right now but no , it would look more like this.

``````func get_input():
if velocity.length() == 0:
velocity = Vector2.ZERO
if Input.is_action_pressed("right"): # rest of your input script
``````

velocity cant be zero till after the length test because that is asking if the player has velocity.

``````extends KinematicBody2D

var velocity = Vector2.ZERO
var direction = Vector2.ZERO
var speed = 400

func get_input():
direction = Vector2.ZERO
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_right"):
direction.x += 1

func _physics_process(_delta):
if velocity.length() == 0:
get_input()
velocity = Vector2.ZERO
velocity += direction * speed
velocity = move_and_slide (velocity)
``````

ArthurER | 2020-10-05 05:01

Thank you very much it works great

r1diq | 2020-10-05 09:31