Its possible for your character to move past the point entirely before it checks to see if it has made it, I’m thinking that’s the problem. You can fix it in a few ways, the easiest would be to give the character a bit of wiggle room around the point, or a check to see if it has made it or passed it instead of just checking to see if it’s there. The best way to fix the issue will depend on your use case.
Edit:
This video has a working implementation.
In this they check to see if the current position is within an acceptable distance from the target and only move if they’re not (giving themself a bit of wiggle room).
The position of objects are decimal numbers and not integers. Meaning you could as well expect a position of Vector2(2.48737426, 5.2653553), when it is not the case for the position of mouse.
In programming, it is recommended not to compare two decimals (floating point numbers) directly, instead what you should do is to check if the position of mouse is inside a range close to the target.
Yes, I understand and I have already done something similar but it didn’t work, and the code from the video doesn’t work at all, although they are similar, maybe you could give your own example?
I recommend using a new variable called tolerance and fiddle with it. You should never check if two floating point numbers are equal or not, you should instead do something like: if position.distance_to(target) > tolerance:
tolerance can be something like 1.0 at first, but try different values.
Im not going to give you code for you to take and use, that’s not how you should learn programming and it’s not helpful if I give it to you. If you’ve edited your code to be more in line with the video and it’s not working then send it, I’m more than happy helping troubleshoot it.
There’s nothing inherently wrong about providing example code, you learn quite a lot by studying code other people wrote, but in this case, a video with working code was already provided, and it was seemingly ignored, or because it didn’t work right away, it was assumed to be wrong by the OP.
yes you were right, it’s better to achieve everything yourself, but I also want to thank everyone for their help, here’s the code
extends CharacterBody2D
var speed = 150
var target = position
var distance
func _physics_process(delta: float) → void:
if Input.is_action_just_pressed(“click”):
target = get_global_mouse_position()
velocity = (target-position).normalized() * speed
var distance = sqrt((position.x-target.x)(position.x-target.x)+(position.y-target.y)(position.y-target.y))
if distance < 3:
velocity = Vector2.ZERO
move_and_slide()
I decided to do more math and yes, everything worked out