AI now follows player

This commit is contained in:
Paul Black 2022-03-06 16:36:53 -07:00
parent 6d5e7d23da
commit 39b8564261

View file

@ -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