I'm new to Godot and I'm trying to code fire bullets, when I use area2d type, there's an error in the 'apply impulse' section like "invalid call. Nonexistent function 'apply_impulse' in base 'Area2D(Bullet.gd)'. What do I have to do?
here's my code :
(Bullet.gd)
extends Area2D
export (PackedScene) var bullet
func _on_KillTimer_timeout():
pass # Replace with function body.
func _on_Bullet_body_entered(body):
if !body.is_in_group("player"):
queue_free()
then my code in gun.gd :
extends Sprite
export var bullet_speed = 1000
export var fire_rate = 0.2
var bullet = preload("res://Characters/Player/Bullet.tscn")
var can_fire = true
func _process(delta):
var mouse_direction: Vector2 = (get_global_mouse_position() - global_position).normalized()
look_at(get_global_mouse_position())
if Input.is_action_pressed("fire") and can_fire:
var bullet_instance = bullet.instance()
bullet_instance.position = $BulletPoint.get_global_position()
get_parent().screen_shaker._shake(0.2, 2)
bullet_instance.rotation_degrees = rotation_degrees + rand_range(0.1, 0.1)
bullet_instance.apply_impulse(Vector2(), Vector2(bullet_speed, 0).rotated(rotation))
get_tree().get_root().add_child(bullet_instance)
can_fire = false
yield(get_tree().create_timer(fire_rate),"timeout")
can_fire = true