# Why my code is wrong?

Attention Topic was automatically imported from the old Question2Answer platform.

I want to limit movement of object by 1000 units in one turn. This object is singleton and by button click I set a distance1 = 0. But after click on button player won’t move.
Output:
Distance = 0
Distance = 0
Distance = 69.327126
Distance = 1533.144043

In some reason there are two outputs of distance1. One is 69.327126 and another is 1533.144043.

Main class

``````extends KinematicBody2D

var pos
var distance1 = 0

var target = Vector2()

func _input(event):
if event.is_action_pressed("RightClick"):
target = get_global_mouse_position()
print("Distance = " + str(distance1))

func _physics_process(delta):
if distance1 > 1000:
target = position
if distance1 <= 1000:
if position.distance_to(target) > 5:
position = get_global_mouse_position()
distance1 = distance1 + pos.distance_to(target)
pos = position

pos = position
target = position
pass # Replace with function body.
``````

Button class

``````extends Button

func _on_Make_Turn_pressed():
Player.distance1 = 0
Player.pos = Player.position
Player.target = Player.position
pass # Replace with function body.
``````

I cannot reproduce the problems you describe. Can you upload an example project?

after click on button player won’t move.

Clicking the button won’t move the player but reset `distance1` back to zero. So I can right-click a few times and the player will move until `distance1` becomes bigger than 1000, click the button once and after that a right-click will move the player again.

It’s worth pointing out though that the first move is always possible, regardless of the distance between the current position and the target set by the mouse click, simply because you’re not checking how large that distance actually is.

there are two outputs of distance1

Not for me.

njamster | 2020-06-06 13:13

Make sure to review the How to use this Q&A before posting questions. Right now your title is not descriptive, which makes it less likely for people to answer! For movement of a KinematicBody2D object, you’re right that you should be using `_physics_process`, but you should also be using either `move_and_slide` or `move_and_collide` for moving, instead of directly changing the position.
For the multiple outputs of distance, right now you’re only changing the value of `distance1` with the line `distance1 = distance1 + pos.distance_to(target)`, so `distance1` will only ever get larger with each time `_physics_process` is called (until it’s greater than 1000).