From 80a24657798296cdcd272decc8ad2f2fb59257cd Mon Sep 17 00:00:00 2001 From: Techwizz Date: Mon, 7 Mar 2022 20:13:43 -0700 Subject: [PATCH] AI now shoots, needs more randomization --- Objects/Ai-tank/tank00.gd | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Objects/Ai-tank/tank00.gd b/Objects/Ai-tank/tank00.gd index 4f26c1c..76cfba5 100644 --- a/Objects/Ai-tank/tank00.gd +++ b/Objects/Ai-tank/tank00.gd @@ -1,10 +1,21 @@ extends KinematicBody +export (PackedScene) var Bullet = preload("res://Objects/Bullet/Bullet.tscn") +var _bullettimer = null var speed = 10 var gravity = 100 var velocity = Vector3.ZERO onready var player = get_node("/root/Spatial/PlayerTank") +func _ready(): ##For bullet timer + _bullettimer = Timer.new() + add_child(_bullettimer) + + _bullettimer.connect("timeout", self, "_bullet_fire") + _bullettimer.set_wait_time(1.0) + _bullettimer.set_one_shot(false) # Make sure it loops + _bullettimer.start() + func _physics_process(delta): velocity = Vector3.ZERO @@ -13,3 +24,10 @@ func _physics_process(delta): velocity = (player.transform.origin - transform.origin).normalized() * speed # make velocity direction equal to player direction velocity = move_and_slide(velocity) # Move AI towards player + +func _bullet_fire(): + if player != null: + var b = Bullet.instance() + owner.add_child(b) + b.transform = $CollisionShape/Tank/BulletGenerator.global_transform + b.velocity = -b.global_transform.basis.z * b.muzzle_velocity