4.3 Godot Version
I made a piece of code for a ball in my game about a copy of Atari breakout. I added a piece of code that releases the ball after you click the screen, because at the beginning of the game, your ball will follow the paddle. The problem is, whenever I release it, it always tends to go a X position a few frames before. I can’t send a video of it since I am a new user, but I can send the code. I’m a newcomer to Godot so help would be really appreciated and if you could also teach me how to avoid the problem again then that would be even more appreciated.
extends RigidBody2D
var release = false
var clickPosition = Vector2()
func _process(delta: float) -> void:
if not Input.is_action_just_pressed("click_default") and release == false:
position.x = %Player.position.x
position.y = %Player.position.y - 25
elif Input.is_action_just_pressed("click_default") and release == false:
release = true
position.x = get_global_mouse_position().x
linear_velocity.y = -400
linear_damp = -0.1
print(linear_damp)```
You seem to be mixing position and global position. You are also using a negative damping which should be positive. It is a slowing factor but it should be described with a positive number really.
func _process(delta: float) -> void:
if not release:
if Input.is_action_just_pressed("click_default"):
release = true
global_position.x = get_global_mouse_position().x
linear_velocity.y = -400
linear_damp = 0.1
else:
global_position = %Player.global_position + Vector2(0, -25)
So although the above is a bit cleaner, I don’t think this will solve your problem.
Usually RigidBodies are moved in _physics_process. Also, I think if you move this into _physics_process, then it might fix it. Process can be called many times a frame, as fast as the CPU allows, whereas physics process is only called once per frame, or at your physics frames per second settings. There are many other differences too, so try to use the right one.
Hope that helps in some way.
Thanks for helping me! I want to clarify that I set the dampening to a negative value to speed up the ball slightly. Thanks a lot again, I had to repost this after a while because everybody ignored the first one. ![:sweat_smile: :sweat_smile:](https://forum.godotengine.org/images/emoji/twitter/sweat_smile.png?v=12)
1 Like