Move only when not moving

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

Hi all,

So have a ball that I can launch kind of like a angry birds situation, but when I launch it, I can basically grab it in the air and launch again, is there a way that I can stop that and only make ball draggable when its not moving?

Your code please?

SIsilicon | 2018-12-19 21:05

We really need some of the code to answer this.

p7f | 2018-12-20 11:37

Hi all so this is the code I got so far, I want to add a if not moving statement as well:

extends RigidBody2D

var dragging
var drag_start = Vector2()

func _input(event):
	if event.is_action_pressed("click") and not dragging:
		dragging = true
		drag_start = get_global_mouse_position()
	if event.is_action_released("click") and dragging:
		dragging = false
		var drag_end = get_global_mouse_position()
		var dir = drag_start - drag_end
		apply_impulse(Vector2(), dir * 5)

rafah | 2018-12-20 14:19

:bust_in_silhouette: Reply From: Antilo

define two state,… dragable or not… if dragrable is true it can be drag if not, not…

i wrote a pseudo-code…

var dragable 
 
func _ready():
    dragable = true
    
func launch_ball():
    if not dragable:
        pass
    else:
       #take the ball and launche it ...  and... 
       ##### change the dragable to false
       dragable= false

#### When ball animation is finished make dragagle again
_on_animation_finised(anim):
     If anim="ball_lauch":
          dragable = true
:bust_in_silhouette: Reply From: p7f

I would try something like this:

extends RigidBody2D

var dragging
var drag_start = Vector2()
var launched = false

func _input(event):
    if event.is_action_pressed("click") and not dragging and not launched:
        dragging = true
        drag_start = get_global_mouse_position()
    if event.is_action_released("click") and dragging:
        dragging = false
        launched = true
        var drag_end = get_global_mouse_position()
        var dir = drag_start - drag_end
        apply_impulse(Vector2(), dir * 5)

However, i don’t know if once launched, it can be launched again or (like in angry birds) you will launch another one. In that case you should set launched to false when the movement ends or something. Perhaps you can use your “dragging” variable for this. Try not to update it to false when released, unless you will need to launch it again after.

Yes I need to launch again after it stops moving and only when not moving, any pointers? I tried setting to false but it just lets me do it all the time, I would appreciate any ideas.

rafah | 2019-01-02 19:30