Godot Version
4
Question
`
So this code is when i click my mouse the bomb gonna go to the mouse position and stop there. The code work but only at the right side. When i move my mouse cursor to the left side its dont work pls help
4
`
So this code is when i click my mouse the bomb gonna go to the mouse position and stop there. The code work but only at the right side. When i move my mouse cursor to the left side its dont work pls help
remove the line
if position >= mousepos:
velocity = Vector2.ZERO
and see if it work or not
alrady try that its work but the bomb wont stop at the mouse location its continue flying away and wont stop
you have 2 solutions: if your bomb is slow enough you can try:
if position == mousepos:
velocity = Vector2.ZERO
or implement fiction that slows the bomb down
if position != mousepos:
velocity = move_toward(velocity, 0, 80) //80 is the slow down rate(fiction) tweak around to find out what best for your game
you can use both at the same time
So i tried the first one and its still the same, its doesnt work. The second one the game give an error. its say “argument one from move_toward() should be float but its Vector2”
Here’s the Vector2 version, with the acceleration (80) per-second instead of per-frame
velocity = velocity.move_toward(Vector2.ZERO, 80 * delta)
It would help to know what you mean by “its don’t work” You must explain what you expected to happen vs what really happens, without both pieces of information it’s impossible to know what you need help with.
I think you need to use an actual distance check, using ==
on floats rarely ever works, especially a Vector2.
if position.distance_squared_to(mousepos) > 25*25:
velocity = direction.normalized() * speed
else:
velocity = velocity.move_toward(Vector2.ZERO, 800 * delta) # higher friction
Make sure to paste code instead of a screenshot
still not working
make sure to also paste your updated code. It’s rather difficult to help if I have no idea what is not working
uh so I have character and they have right side and left side right. And the bomb that I coded only stop at the mouse cursor position when my mouse cursor is on the right side of the screen but it doesnt work when I move my left mouse cursor at the left side of the screen.
哦
第一张图片里的第9行与第10行交换位置,记得缩进
第二张图片里的positon全部改成global_position
Try using global_position
instead of position
in your second script
What does “it doesn’t work” mean? How does it not work? I can understand you want it to stop at the mouse cursor position, like the right side, but what does it really do? Does it fly off the screen? Does it not move at all? Does it crash the game?
Please paste your updated code too.
its stop like stand still and sometime its fly through the game
already tried that
func _physics_process(delta: float) -> void:
mousePos = get_global_mouse_position()
if position.distance_to(mousePos) > 1.0: # Check if the distance is more than a small threshold
direction = (mousePos - position).normalized()
velocity = direction * speed
else:
velocity = Vector2.ZERO
Is this what you what to happen? It’s hard to tell without a clear concise explanation and going off just the pictures on your post…
Also, I moved mouse position update into the function because I assume you want to update where the mouse is every frame rather than just on ready?