How do I apply an impulse on a keypress?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jacobb
:warning: Old Version Published before Godot 3 was released.

I’m trying to make an object go upwards when a key is pressed - this code doesn’t work:

extends Spatial

var flprop
func _ready():
	flprop = get_node("flprop")
	self.set_process(true)

func _process(delta):
	if Input.is_action_pressed("drone_up"):
		flprop.apply_impulse(Vector3(0, 0, 0), Vector3(0, 10, 0))

but if I move the flprop.apply_impulse() line into the _ready function, the object does have the force applied.

Apply a positioned impulse (which will be affected by the body mass
and shape). This is the equivalent of hitting a billiard ball with a
cue: a force that is applied once, and only once. Both the impulse and
the offset from the body origin are in global coordinates.

Maybe use flprop.apply_impulse(flprop.get_traslation(), Vector3(0, 10, 0))?

genete | 2016-06-15 21:36

:bust_in_silhouette: Reply From: ericdl

Your code works as it should, working sample project link below. Did you ensure “drone_up” is defined in your input map?

Project link: https://drive.google.com/file/d/0BwnfZQAEnciAVFRaU2JNalYyVHc/view?usp=sharing

If I add the line print("Test") to the input if statement, the line gets printed, so I know the key press is being registered

jacobb | 2016-06-16 06:34

Can you see the flprop node on screen and confirm it is there? And when you key press “drone_up” do any error messages appear in the debugger?

If possible, would you mind posting a link to your scene or project?

ericdl | 2016-06-16 14:20

Fixed it - flprop was hidden. Thank you!!

jacobb | 2016-06-16 19:34