Apply_impulse not applying any force

Godot Version

4.2.1

Question

How do i get apply impulse to work?

Problem

The bullet spawns below barrel and falls down

Code

	apply_impulse(Vector2.ZERO, Vector2(bullet_speed, 0).rotated(rotation))

I think what you want in this case, to shoot the bullet, is apply_force

ah alright

doesn’t work

Dang. Can you post your full code? Or put your project on GitHub?

this is the code for the gun

extends Sprite2D

var can_fire = true
var bullet = preload("res://bullet.tscn")
	
	
func _process(_delta):
	if Input.is_action_just_pressed("Fire") and can_fire:
		var Bullet_instance = bullet.instantiate()
		Bullet_instance.position =  $Marker2D.get_global_position()
		Bullet_instance.rotation = get_angle_to(get_global_mouse_position())
		get_parent().add_child(Bullet_instance)
		can_fire = false
		await get_tree().create_timer(0.3).timeout
		can_fire = true

Apply_impulse first parameter is the impulse force, and you are setting it to zero, giving it no force.

how would i do that without

vector2.ZERO is short for vector2 0,0

The second parameter is the position of the applied force. You don’t want that, changing this would apply rotation to the object.
This is probably more in line with what you want:

	apply_impulse(Vector2(bullet_speed, 0).rotated(rotation))

The first parameter is the actual impulse force.


You are getting the parameters confused.
You might want to check the docs more carefully.

2 Likes

wow this worked thank you so much!