Is it possible to throw a 3D rigidbody with a mouse action or touchscreen drag?

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

I am working on a simple 3D beer pong game. Currently, I have it working and the apply_impluse function sends the ball flying into the cup. But I was wondering if there is a way to be able to throw the ball using a mouse gesture or screen drag.

There’s an option in input to check for a screen drag. I would elaborate more, however, I’m on mobile.

Ertain | 2019-03-15 23:48

:bust_in_silhouette: Reply From: flurick

What kind of gesture are we talking here? If its enough with one swipe to get a force to multiply apply_impulse with, you can simply get the speed parameter from the input event.

extends ...

var force = 0
func _input(event):
	if event is InputEventScreenDrag:
		force = event.speed

Hey! So the gesture would be like a click and drag (or swipe for touch screen) for a throwing the ball. My current code looks like this,

func _on_Ball_input_event(camera, event, click_position, click_normal, shape_idx):
    if event.is_action_pressed("Click"):
    		force = event.speed

I tired implmeting this, but i get the error Invalid get index 'speed' (on base: 'Input EventMosueButton')

Over all, I want the user to beable to click and drag the ball like a throwing action, then eventually add touch screen support.

SpencerW | 2019-03-16 16:47

Well, I figured out what i was doing wrong.

if event is InputEventMouseMotion:
    force = event.speed

This works to get said speed variable!

SpencerW | 2019-03-16 17:07