소스 검색

:tada: Implement camera tracking

DricomDragon 1 년 전
부모
커밋
ac030f6856
3개의 변경된 파일50개의 추가작업 그리고 5개의 파일을 삭제
  1. 7 0
      first3d/component/entity/walker/walker.gd
  2. 35 0
      first3d/run/levels/camera_tracker.gd
  3. 8 5
      first3d/run/levels/infinite_level.tscn

+ 7 - 0
first3d/component/entity/walker/walker.gd

@@ -1,6 +1,9 @@
 class_name Walker
 extends CharacterBody3D
 
+
+signal focus_required(me: Node3D)
+
 ## How fast the player moves in meters per second.
 @export var speed = 14
 ## Convert an axis from [-1, -1] to rad
@@ -18,6 +21,10 @@ var _vehicle: Node3D = null
 @onready var _vehicle_range: RayCast3D = $VehicleRange
 
 
+func _ready() -> void:
+	focus_required.emit(self)
+
+
 ## Jump if is on floor
 func trigger_jump() -> void:
 	if is_on_floor():

+ 35 - 0
first3d/run/levels/camera_tracker.gd

@@ -0,0 +1,35 @@
+class_name CameraTracker
+extends Camera3D
+## Track object in subject view
+
+
+@onready var _max_distance: float = 10
+
+var _tracked_node: Node3D
+
+
+func track(node: Node3D) -> void:
+	_tracked_node = node
+
+
+func _process(_delta: float) -> void:
+	if _tracked_node == null:
+		return
+
+	look_at(_tracked_node.get_global_position())
+
+	_adjust_distance()
+
+
+func _adjust_distance() -> void:
+	var target: Vector3 = _tracked_node.get_global_position()
+	var current_distance: float = position.distance_to(target)
+	print("From ", position, " to ", target, ", there are ", current_distance, "m")
+	if current_distance > _max_distance:
+		print(current_distance, " is above ", _max_distance)
+		var diff: float = current_distance - _max_distance
+		set_position(position + transform.basis * Vector3.FORWARD * diff)
+
+
+func _on_focus_required(node: Node3D) -> void:
+	track(node)

+ 8 - 5
first3d/run/levels/infinite_level.tscn

@@ -1,5 +1,6 @@
-[gd_scene load_steps=7 format=3 uid="uid://7ivipmwaw24t"]
+[gd_scene load_steps=8 format=3 uid="uid://7ivipmwaw24t"]
 
+[ext_resource type="Script" path="res://run/levels/camera_tracker.gd" id="1_qdm47"]
 [ext_resource type="PackedScene" uid="uid://b10k58capm04o" path="res://component/entity/walker/walker.tscn" id="1_x0026"]
 [ext_resource type="PackedScene" uid="uid://bk0s5bn8k08bc" path="res://flow/control/local_input/local_input.tscn" id="2_0i5t8"]
 [ext_resource type="PackedScene" uid="uid://bcpri2x5afoyb" path="res://component/entity/box/box.tscn" id="2_e8xto"]
@@ -27,13 +28,14 @@ mesh = SubResource("QuadMesh_dt8nl")
 transform = Transform3D(1, 0, 0, 0, 0.734358, 0.678762, 0, -0.678762, 0.734358, -33.1669, 12.6773, 15.7238)
 shadow_enabled = true
 
+[node name="CameraTracker" type="Camera3D" parent="."]
+transform = Transform3D(1, 0, 0, 0, 0.944949, 0.327218, 0, -0.327218, 0.944949, -10, 11.1852, 3.2779)
+current = true
+script = ExtResource("1_qdm47")
+
 [node name="MainWalker" parent="." instance=ExtResource("1_x0026")]
 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10, 9, 0)
 
-[node name="CameraPlayer" type="Camera3D" parent="MainWalker"]
-transform = Transform3D(1, 0, 0, 0, 0.944949, 0.327218, 0, -0.327218, 0.944949, 0, 2.18524, 3.2779)
-current = true
-
 [node name="Control" parent="MainWalker" instance=ExtResource("2_0i5t8")]
 
 [node name="NpcWalker1" parent="." instance=ExtResource("1_x0026")]
@@ -56,6 +58,7 @@ transform = Transform3D(0.477312, 0.527326, -0.702923, -0.741391, 0.671074, 0, 0
 [node name="TinyPlane2" parent="." instance=ExtResource("4_tbpfc")]
 transform = Transform3D(-0.589079, -0.650805, -0.478999, -0.741391, 0.671074, 1.34747e-08, 0.321443, 0.355125, -0.877816, -10.5959, 3.94108, -6.48481)
 
+[connection signal="focus_required" from="MainWalker" to="CameraTracker" method="_on_focus_required"]
 [connection signal="dir_changed" from="MainWalker/Control" to="MainWalker" method="_on_dir_changed"]
 [connection signal="get_in_action" from="MainWalker/Control" to="MainWalker" method="_on_get_in_action"]
 [connection signal="main_action" from="MainWalker/Control" to="MainWalker" method="_on_main_action"]