Created Enemy to track player's last known location
This commit is contained in:
23
Assets/components/enemy.tscn
Normal file
23
Assets/components/enemy.tscn
Normal file
@ -0,0 +1,23 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://cadrciw8ywini"]
|
||||
|
||||
[ext_resource type="Script" path="res://Assets/scripts/Enemy.gd" id="1_fdjbh"]
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_wwg7y"]
|
||||
albedo_color = Color(1, 0, 0, 1)
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_fhbi7"]
|
||||
material = SubResource("StandardMaterial3D_wwg7y")
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_kerfs"]
|
||||
|
||||
[node name="Enemy" type="CharacterBody3D"]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.00193, 0)
|
||||
script = ExtResource("1_fdjbh")
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
|
||||
mesh = SubResource("CapsuleMesh_fhbi7")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("CapsuleShape3D_kerfs")
|
||||
|
||||
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
|
||||
26
Assets/scripts/Enemy.gd
Normal file
26
Assets/scripts/Enemy.gd
Normal file
@ -0,0 +1,26 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
@onready var nav_agent = $NavigationAgent3D
|
||||
|
||||
@export var SPEED:float = 3.0
|
||||
|
||||
var prev_known_location = Vector3(0,0,0)
|
||||
|
||||
func _ready():
|
||||
update_target_location(global_transform.origin)
|
||||
|
||||
func _process(delta):
|
||||
if prev_known_location != GlobalSettings.Player_Last_Location:
|
||||
prev_known_location = GlobalSettings.Player_Last_Location
|
||||
update_target_location(GlobalSettings.Player_Last_Location)
|
||||
|
||||
func _physics_process(delta):
|
||||
var current_location = global_transform.origin
|
||||
var next_location = nav_agent.get_next_path_position()
|
||||
var new_velocity = (next_location - current_location).normalized() * SPEED * delta
|
||||
|
||||
velocity = new_velocity
|
||||
move_and_slide()
|
||||
|
||||
func update_target_location(target_location):
|
||||
nav_agent.set_target_position(target_location)
|
||||
Reference in New Issue
Block a user