Problem about control Rogidbody2D ball in the air

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

Hi, I try to make a ball that can bounce using Rigidbody2D. Code come from Kidscancode example

extends RigidBody2D

var dragging
var drag_start = Vector2()
var drag_end = Vector2()

func _input(event):
mode = RigidBody2D.MODE_RIGID
if event.is_action_pressed("LMB") and not dragging:
	dragging = true
	drag_start = self.position
	
if event.is_action_released("LMB") and dragging:
	dragging = false
	drag_end = get_global_mouse_position()
	var dir = drag_start - drag_end
	apply_impulse(Vector2(), dir * -6)

but when I try to control this ball in the air. I think the ball have some glitch that I don’t want it.
here is simple

I need to control the ball to a force #2 vector but when the ball fly in the air, the ball have old force (I named that force #1)
Then, the ball will go to a result vector (but I don’t want the ball go to that way)

I think I should remove/clear force #1, or use other way to control the ball (and get the better result)
what should I do? edit my script? or use other way. and what way I can use?
help me please

Can you share the code you’re using? It would also help if you elaborated a little on your desired behavior. Do you want the ball to zero-out force #1 as soon as the user inputs force #2, or do you want them to fade from one to the other somehow? It might be a little jarring for the user to have the ball jerk back and forth without respect to inertia.

DDoop | 2020-07-09 21:28

I’m not quite getting what the problem is but you might need to use add_force(). apply_impulse() is like kicking a ball, if the ball is falling and you kick it dead center back up, it’ll go up not sideways.

Magso | 2020-07-09 22:20

I think I share my code already.
I think I want the ball to zero-out force #1 as soon as the user inputs force #2.

MoNoSpaze | 2020-07-10 01:47

You can zero the velocity to cancel out physics movement like that.

linear_velocity = Vector2.ZERO
apply_impulse(...)

Magso | 2020-07-10 11:34