Why my code is wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MailFolder345

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.
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

func _ready():
    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

:bust_in_silhouette: Reply From: patrice

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.

Highly recommend checking out this tutorial for getting the hang of the different movement and input options you have with a KinematicBody2D.

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).

But I also can change distance1 with button. In Code of Button I reset distance1 and set it to 0 and this must let the Player to move, but Player won’t move. Thank you for tip, I know about move and slide but I don’t want to use it. I just want, my player teleport to new location every time I press the right mouse.

MailFolder345 | 2020-06-06 07:51