feat: ninja follows player

This commit is contained in:
Sara Gerretsen 2026-03-19 13:43:51 +01:00
parent 0f2eb97118
commit edf06623b3
15 changed files with 144 additions and 39 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1,13 +1,17 @@
extends BehaviourAction
@export var target_distance : float = 1.2
@onready var body := get_behaviour_tree().get_parent() as CharacterBody3D
@onready var anim := get_behaviour_tree().get_node("%AnimationPlayer") as AnimationPlayer
func _enter() -> int:
var full_angle := body.global_basis.z.signed_angle_to((body.global_position - body.target.global_position), Vector3.UP)
body.rotate_y(full_angle)
anim.play("run", .1)
if body.target == null or body.target.global_position.distance_to(body.global_position) > 1.2:
if body.target == null or body.target.global_position.distance_to(body.global_position) > target_distance:
var difference : Vector3 = body.global_position - body.target.global_position
var z_basis : Vector3 = body.global_basis.z
var full_angle := Vector3(z_basis.x, 0, z_basis.z).signed_angle_to(Vector3(difference.x, 0, difference.z).normalized(), Vector3.UP)
body.rotate_y(full_angle)
anim.play("run", .1)
return Fail
else:
return Success

View file

@ -13,12 +13,8 @@ script = ExtResource("1_ki4y0")
[node name="FindWeapon" type="BehaviourAction" parent="." unique_id=458518105]
script = ExtResource("2_5438c")
[node name="BehaviourAlwaysSuccess" type="BehaviourAlwaysSuccess" parent="." unique_id=631129208]
[node name="BehaviourSequence" type="BehaviourSequence" parent="BehaviourAlwaysSuccess" unique_id=545263859]
[node name="MoveToItem" type="BehaviourAction" parent="BehaviourAlwaysSuccess/BehaviourSequence" unique_id=1081201512]
[node name="MoveToItem" type="BehaviourAction" parent="." unique_id=1081201512]
script = ExtResource("3_5438c")
[node name="PickupItem" type="BehaviourAction" parent="BehaviourAlwaysSuccess/BehaviourSequence" unique_id=2083134526]
[node name="PickupItem" type="BehaviourAction" parent="." unique_id=2083134526]
script = ExtResource("3_62xhk")

View file

@ -0,0 +1,17 @@
extends BehaviourAction
@export var target_distance : float = 1.2
@onready var body := get_behaviour_tree().get_parent() as CharacterBody3D
@onready var anim := get_behaviour_tree().get_node("%AnimationPlayer") as AnimationPlayer
func _enter() -> int:
if body.target == null or body.target.global_position.distance_to(body.global_position) > target_distance:
var difference : Vector3 = body.global_position - body.target.global_position
var z_basis : Vector3 = body.global_basis.z
var full_angle := Vector3(z_basis.x, 0, z_basis.z).signed_angle_to(Vector3(difference.x, 0, difference.z).normalized(), Vector3.UP)
body.rotate_y(full_angle)
anim.play("run", .1)
return Success
else:
return Fail

View file

@ -0,0 +1 @@
uid://bm6unenoiyy5h

View file

@ -0,0 +1,9 @@
extends BehaviourAction
@export var animation_name : String = "RESET"
@onready var anim := get_behaviour_tree().get_node("%AnimationPlayer") as AnimationPlayer
func _enter() -> int:
anim.play(animation_name, .1)
return Success

View file

@ -0,0 +1 @@
uid://b2s7gavo263mo

View file

@ -7,6 +7,7 @@
[ext_resource type="PackedScene" uid="uid://box61yo165gs6" path="res://behaviours/branches/sequence_find_weapon.tscn" id="4_d53py"]
[ext_resource type="PackedScene" uid="uid://cgkrqn4f7ko13" path="res://behaviours/branches/selector_attack_animation.tscn" id="7_bmb0k"]
[ext_resource type="Script" uid="uid://cgcajnncoittb" path="res://behaviours/approach_target.gd" id="7_tx7q7"]
[ext_resource type="Script" uid="uid://b2s7gavo263mo" path="res://behaviours/npc_idle.gd" id="8_tx7q7"]
[sub_resource type="GDScript" id="GDScript_xqvpn"]
resource_name = "Wait"
@ -22,17 +23,6 @@ func _execute() -> int:
return Success if $Timer.time_left == 0 else Running if not body.hurt_high else Fail
"
[sub_resource type="GDScript" id="GDScript_41oev"]
resource_name = "EnemyIdle"
script/source = "extends BehaviourAction
@onready var anim := get_behaviour_tree().get_node(\"%AnimationPlayer\") as AnimationPlayer
func _enter() -> int:
anim.play(\"RESET\", .1)
return Success
"
[node name="BehaviourTree" type="BehaviourTree" unique_id=82050956]
[node name="BehaviourRepeater" type="BehaviourRepeater" parent="." unique_id=1340157903]
@ -85,6 +75,6 @@ script = ExtResource("3_1fc8h")
script = ExtResource("7_tx7q7")
[node name="Idle" type="BehaviourAction" parent="BehaviourRepeater/BehaviourSelector" unique_id=2051783162]
script = SubResource("GDScript_41oev")
script = ExtResource("8_tx7q7")
[editable path="BehaviourRepeater/BehaviourSelector/SequenceHitHigh"]

View file

@ -14,4 +14,4 @@ script = ExtResource("1_ibr75")
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=2035583636]
shape = SubResource("CylinderShape3D_uxov2")
debug_color = Color(0.5434826, 0.40605906, 1, 0.41960785)
debug_color = Color(0.54509807, 0.40784314, 1, 1)

View file

@ -98,15 +98,15 @@ func _process(_delta):
height = 3.3642578
radius = 9.955078
[node name="bot" unique_id=1098018666 groups=["EnemyHitbox"] instance=ExtResource("1_882u1")]
[node name="bot" unique_id=629825830 groups=["EnemyHitbox"] instance=ExtResource("1_882u1")]
collision_layer = 2
collision_mask = 3
script = SubResource("GDScript_882u1")
[node name="Mesh" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="0" unique_id=1628401802]
[node name="Mesh" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1098642975) index="0" unique_id=1675779214]
material_override = SubResource("StandardMaterial3D_41oev")
[node name="HandR" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="1" unique_id=957498611]
[node name="HandR" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1098642975) index="1" unique_id=957498611]
transform = Transform3D(0.015967373, -0.083946444, 0.9963424, -0.17989334, -0.98045003, -0.07972447, 0.9835564, -0.17796235, -0.030756809, 0.27221423, 0.7951686, 0.024477992)
bone_name = "hand.r"
bone_idx = 10
@ -131,7 +131,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.7755576e-16, 0.10578142, 2
script = ExtResource("3_3hb37")
damage_group = "PlayerHitbox"
[node name="HandL" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="2" unique_id=1761081841]
[node name="HandL" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1098642975) index="2" unique_id=1761081841]
transform = Transform3D(-0.039434362, 0.07924447, -0.9960749, 0.12679999, -0.9883944, -0.083653376, -0.99114394, -0.12960118, 0.028928574, -0.2710728, 0.7714727, 0.0945672)
bone_name = "hand.l"
bone_idx = 7
@ -150,7 +150,7 @@ transform = Transform3D(1.0000169, -1.5795231e-06, -3.874302e-07, 8.046627e-07,
shape = SubResource("BoxShape3D_we7g8")
debug_color = Color(0.9966913, 0, 0.17880186, 1)
[node name="AnimationPlayer" parent="." index="1" unique_id=413547776]
[node name="AnimationPlayer" parent="." index="1" unique_id=1392290156]
unique_name_in_owner = true
root_motion_track = NodePath("Character/Skeleton3D:root")

View file

@ -0,0 +1,75 @@
[gd_scene format=3 uid="uid://cu1nh3rbcnn3w"]
[ext_resource type="PackedScene" uid="uid://hirlnhjbqv2u" path="res://assets/characters/bot.blend" id="1_fssyu"]
[ext_resource type="Script" uid="uid://bm6unenoiyy5h" path="res://behaviours/follow_target.gd" id="2_klwnj"]
[ext_resource type="Script" uid="uid://b2s7gavo263mo" path="res://behaviours/npc_idle.gd" id="3_klwnj"]
[ext_resource type="PackedScene" uid="uid://deuif6d8spece" path="res://objects/awareness_area.tscn" id="3_tpgvx"]
[sub_resource type="GDScript" id="GDScript_klwnj"]
resource_name = "NinjaBody"
script/source = "extends CharacterBody3D
@export var player : Node3D
var target : Node3D
func _physics_process(delta : float):
var motion : Vector3 = %AnimationPlayer.get_root_motion_position() / delta
velocity = motion.x * global_basis.x + motion.z * global_basis.z + velocity.y * Vector3.UP
velocity += get_gravity() * delta
move_and_slide()
"
[sub_resource type="GDScript" id="GDScript_tpgvx"]
resource_name = "SelectPlayer"
script/source = "extends BehaviourAction
@onready var body := get_behaviour_tree().get_parent() as CharacterBody3D
func _enter() -> int:
body.target = body.player
return Success if body.player else Fail
"
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_lyptg"]
albedo_color = Color(0.4745098, 0.16862746, 0.61960787, 1)
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_lyptg"]
radius = 0.30273438
[node name="bot" unique_id=1341242765 instance=ExtResource("1_fssyu")]
script = SubResource("GDScript_klwnj")
[node name="BehaviourTree" type="BehaviourTree" parent="." index="0" unique_id=1843075231]
[node name="BehaviourRepeater" type="BehaviourRepeater" parent="BehaviourTree" index="0" unique_id=1408557668]
[node name="BehaviourSelector" type="BehaviourSelector" parent="BehaviourTree/BehaviourRepeater" index="0" unique_id=487995934]
[node name="FollowPlayer" type="BehaviourSequence" parent="BehaviourTree/BehaviourRepeater/BehaviourSelector" index="0" unique_id=1977466711]
[node name="SelectPlayer" type="BehaviourAction" parent="BehaviourTree/BehaviourRepeater/BehaviourSelector/FollowPlayer" index="0" unique_id=92830272]
script = SubResource("GDScript_tpgvx")
[node name="FollowTarget" type="BehaviourAction" parent="BehaviourTree/BehaviourRepeater/BehaviourSelector/FollowPlayer" index="1" unique_id=282888066]
script = ExtResource("2_klwnj")
target_distance = 3.0
[node name="Idle" type="BehaviourAction" parent="BehaviourTree/BehaviourRepeater/BehaviourSelector" index="1" unique_id=1038185599]
script = ExtResource("3_klwnj")
animation_name = "duck"
[node name="Mesh" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(750883464) index="0" unique_id=233192374]
material_override = SubResource("StandardMaterial3D_lyptg")
[node name="AnimationPlayer" parent="." index="2" unique_id=1278488642]
unique_name_in_owner = true
root_motion_track = NodePath("Character/Skeleton3D:root")
[node name="CollisionShape3D" type="CollisionShape3D" parent="." index="3" unique_id=1579307622]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
shape = SubResource("CapsuleShape3D_lyptg")
[node name="AwarenessArea" parent="." index="4" unique_id=986494999 instance=ExtResource("3_tpgvx")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.92650235, 0)
target_group = "HidingSpot"

View file

@ -98,15 +98,21 @@ var joystick_input : Vector2 = Vector2.ZERO
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func deadzone(value : float) -> float:
if abs(value) < 0.05:
return 0
else:
return value
func _unhandled_input(event : InputEvent):
if event is InputEventMouseMotion:
input = event.relative * 0.002
elif event is InputEventJoypadMotion:
match (event as InputEventJoypadMotion).axis:
JOY_AXIS_RIGHT_X:
joystick_input.x = event.axis_value
joystick_input.x = deadzone(event.axis_value)
JOY_AXIS_RIGHT_Y:
joystick_input.y = event.axis_value
joystick_input.y = deadzone(event.axis_value)
func _process(delta : float):
input += joystick_input * delta * 3.0
@ -132,7 +138,7 @@ size = Vector3(0.33496094, 0.8294678, 0.4711914)
height = 1.8466797
radius = 0.89404297
[node name="bot" unique_id=1098018666 groups=["PlayerHitbox"] instance=ExtResource("1_eqqp1")]
[node name="bot" unique_id=1341242765 groups=["PlayerHitbox"] instance=ExtResource("1_eqqp1")]
collision_layer = 2
collision_mask = 3
script = SubResource("GDScript_eqqp1")
@ -162,10 +168,10 @@ transform = Transform3D(0.981895, -5.3638707e-09, -0.18942553, 5.529141e-09, 1,
current = true
fov = 52.5
[node name="Mesh" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="0" unique_id=1628401802]
[node name="Mesh" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(750883464) index="0" unique_id=233192374]
material_override = SubResource("StandardMaterial3D_f46kd")
[node name="HandR" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="1" unique_id=385862689]
[node name="HandR" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(750883464) index="1" unique_id=385862689]
transform = Transform3D(0.015967373, -0.083946444, 0.9963424, -0.17989334, -0.98045003, -0.07972447, 0.9835564, -0.17796235, -0.030756809, 0.27221423, 0.7951686, 0.024477992)
bone_name = "hand.r"
bone_idx = 10
@ -190,7 +196,7 @@ script = ExtResource("5_f46kd")
damage_group = "EnemyHitbox"
deal_damage = true
[node name="HandL" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="2" unique_id=279793409]
[node name="HandL" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(750883464) index="2" unique_id=279793409]
transform = Transform3D(-0.039434362, 0.07924447, -0.9960749, 0.12679999, -0.9883944, -0.083653376, -0.99114394, -0.12960118, 0.028928574, -0.2710728, 0.7714727, 0.0945672)
bone_name = "hand.l"
bone_idx = 7
@ -208,7 +214,7 @@ transform = Transform3D(1.0000169, -1.5944242e-06, -4.04194e-07, 6.556511e-07, 1
shape = SubResource("BoxShape3D_uxov2")
debug_color = Color(0.9966913, 0, 0.17880186, 1)
[node name="LegR" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(1205224726) index="3" unique_id=2088257762]
[node name="LegR" type="BoneAttachment3D" parent="Character/Skeleton3D" parent_id_path=PackedInt32Array(750883464) index="3" unique_id=2088257762]
transform = Transform3D(0.9947343, 0.10095803, 0.017639676, 0.10222586, -0.98967797, -0.1004331, 0.0073180734, 0.1017075, -0.9947873, 0.14436838, 0.42896914, -0.076926425)
bone_name = "leg.lower.r"
bone_idx = 14
@ -226,7 +232,7 @@ transform = Transform3D(1, -5.960372e-07, -1.0579633e-06, 5.960361e-07, 1, -1.03
shape = SubResource("BoxShape3D_nmc1l")
debug_color = Color(0.9966913, 0, 0.17880186, 1)
[node name="AnimationPlayer" parent="." index="5" unique_id=413547776]
[node name="AnimationPlayer" parent="." index="5" unique_id=1278488642]
unique_name_in_owner = true
root_motion_track = NodePath("Character/Skeleton3D:root")

View file

@ -26,6 +26,7 @@ PlayerHitbox=""
EnemyHitbox=""
Interactive=""
WeaponPickup=""
HidingSpot=""
[input]

View file

@ -5,6 +5,7 @@
[ext_resource type="Texture2D" uid="uid://c2uvr1d00euop" path="res://assets/prototype_textures/Red/texture_01.png" id="2_75bvq"]
[ext_resource type="Texture2D" uid="uid://cx3fxgh11s4vh" path="res://assets/prototype_textures/Orange/texture_01.png" id="2_hyc8p"]
[ext_resource type="PackedScene" uid="uid://1s2obm2l4f1a" path="res://objects/enemy.tscn" id="2_w0s2s"]
[ext_resource type="PackedScene" uid="uid://cu1nh3rbcnn3w" path="res://objects/ninja.tscn" id="4_b3nq0"]
[ext_resource type="PackedScene" uid="uid://djv4uhdtykukm" path="res://objects/arming_sword.tscn" id="5_hyc8p"]
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_upt1h"]
@ -98,10 +99,14 @@ mesh = SubResource("PrismMesh_hyc8p")
transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, -9.232916, 1.4917104, -14.66638)
mesh = SubResource("PrismMesh_hyc8p")
[node name="bot" parent="." unique_id=1195498535 instance=ExtResource("1_upt1h")]
[node name="Ninja" parent="." unique_id=1341242765 node_paths=PackedStringArray("player") instance=ExtResource("4_b3nq0")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 3, -18)
player = NodePath("../Player")
[node name="Player" parent="." unique_id=1098018666 instance=ExtResource("1_upt1h")]
transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, 0, 2.991, -17.607033)
[node name="bot7" parent="." unique_id=1620071502 node_paths=PackedStringArray("path") instance=ExtResource("2_w0s2s")]
[node name="Enemy" parent="." unique_id=629825830 node_paths=PackedStringArray("path") instance=ExtResource("2_w0s2s")]
transform = Transform3D(-1, 0, -8.742278e-08, 0, 1, 0, 8.742278e-08, 0, -1, -9.093315, 0, 1.792202)
path = [NodePath("../Marker3D"), NodePath("../Marker3D2"), NodePath("../Marker3D3"), NodePath("../Marker3D4")]
@ -120,7 +125,7 @@ transform = Transform3D(-0.9973041, 0.063548714, -0.036689803, 5.1930144e-08, 0.
[node name="Marker3D4" type="Marker3D" parent="." unique_id=1864554328]
transform = Transform3D(-0.9973041, 0.063548714, -0.036689803, 5.1930144e-08, 0.5000001, 0.86602545, 0.073379725, 0.86369073, -0.4986521, -7.3572216, 0, -6.262743)
[node name="bot3" parent="." unique_id=1894010791 node_paths=PackedStringArray("path") instance=ExtResource("2_w0s2s")]
[node name="Enemy2" parent="." unique_id=1129420379 node_paths=PackedStringArray("path") instance=ExtResource("2_w0s2s")]
transform = Transform3D(0.3496833, 0, -0.93686795, 0, 1, 0, 0.93686795, 0, 0.3496833, -0.5668974, 0, 16.554804)
path = [NodePath("../Marker3D5"), NodePath("../Marker3D6"), NodePath("../Marker3D7"), NodePath("../Marker3D8")]