0 votes

I want the cannon to shoot at all directions after every second. This current script is of a Canon that shoots my player when he is in it's range :

extends Sprite
var player=null

var rot_sped=0.3
var fire_rate=0.3
var fire_time=0.5
var rang=300


var canonbullet=preload("res://CanonBullet.tscn")

func _physics_process(delta):
    if player==null:
        return
    var dir_to_player=player.global_position-global_position
    rotation=dir_to_player.angle()
    fire_time+=delta
    if fire_time>fire_rate:
        fire_time=0
        fire()


func fire():
    if global_position.distance_to(player.global_position)>rang:
        return
    var canonbullet_inst=canonbullet.instance()
    get_tree().get_root().add_child(canonbullet_inst)
    canonbullet_inst.global_position=global_position
    var dir_to_player=(player.global_position-global_position).normalized()
    canonbullet_inst.move_dir=dir_to_player

func set_player(p):
    player=p


func _on_Area2D_body_entered(body: Node) -> void:
    if "Player" in body.name:
        get_tree().reload_current_scene()
in Projects by (359 points)

1 Answer

0 votes

Hi,

Add a timer node.
Make it one shot with Wait Time of 1 second.
Add a signal for timeout()

In the timeout code, add something like:

func _on_Timer_timeout() -> void:
    move the fire code from _physics_process to here
    $Timer.start()
by (1,629 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories