From 39b8564261859a9757919086edb579c507438436 Mon Sep 17 00:00:00 2001 From: Techwizz Date: Sun, 6 Mar 2022 16:36:53 -0700 Subject: [PATCH] AI now follows player --- Objects/Ai-tank/tank00.gd | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Objects/Ai-tank/tank00.gd b/Objects/Ai-tank/tank00.gd index c678a95..4f26c1c 100644 --- a/Objects/Ai-tank/tank00.gd +++ b/Objects/Ai-tank/tank00.gd @@ -1,19 +1,15 @@ extends KinematicBody -var speed = 15 +var speed = 10 var gravity = 100 var velocity = Vector3.ZERO -# onready var player = find_node("PlayerTank") ## FIXME -onready var player = get_node("/root/Spatial/PlayerTank/CollisionShape") +onready var player = get_node("/root/Spatial/PlayerTank") func _physics_process(delta): - var direction = Vector3.ZERO + velocity = Vector3.ZERO if player != null: # Makes sure player is not an empty variable (not equal to nill/null) - $CollisionShape.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0)) - direction.z -= 1 ### CODE FROM HERE DOWN NEEDS IMPROVEMENT (AI walks up nonstop and not towards player...) - velocity.x = direction.x * speed - velocity.z = direction.z * speed - velocity.y -= gravity * delta - velocity = move_and_slide(velocity, Vector3.UP) + $CollisionShape.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0)) # Look at player + velocity = (player.transform.origin - transform.origin).normalized() * speed # make velocity direction equal to player direction + velocity = move_and_slide(velocity) # Move AI towards player