cast a ray to objects on layer 2 and emit a signal when it changes
authorEduardo <[email protected]>
Sun, 11 Feb 2024 16:54:03 +0000 (17:54 +0100)
committerEduardo <[email protected]>
Sun, 11 Feb 2024 16:54:03 +0000 (17:54 +0100)
player/Camera3D.gd
player/Player.tscn

index e5347aa2a5a60d846476fe0a903e84a6306acad3..bbafc3f521d3a36cf617da58c489c19bf6660750 100644 (file)
@@ -1,13 +1,19 @@
 extends Camera3D
 
+signal object_detected
+
 @export var movEnabled := true
 @export var mouseSensitivity := 0.5
 @export var flyspeed := 100.0
 @export var debug := false
 @export var player: Node3D
 
-var yaw : float = 0.0
-var pitch : float = 0.0
+@onready var ray := $RayCast3D
+
+var is_object_detected := false
+var yaw: float = 0.0
+var pitch: float = 0.0
+
 
 # Called when the node enters the scene tree for the first time.
 func _ready():
@@ -16,6 +22,7 @@ func _ready():
 
        Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
 
+
 func _input(event):
        # release the mouse on esc
        if event.is_action_pressed("ui_cancel"):
@@ -37,16 +44,29 @@ func _input(event):
 
                yaw = fmod(yaw - mouseVec.x * mouseSensitivity, 360)
                pitch = max(min(pitch - mouseVec.y * mouseSensitivity, 90), -90)
-               player.rotation = Vector3(deg_to_rad(pitch), deg_to_rad(yaw), 0)
+               player.rotation = Vector3(0, deg_to_rad(yaw), 0)
+               rotation = Vector3(deg_to_rad(pitch), 0, 0)
+
 
 # Called every frame. 'delta' is the elapsed time since the previous frame.
 func _process(delta):
-       if(Input.is_action_pressed("ui_up")):
+       if Input.is_action_pressed("ui_up"):
                translate(position - global_basis * Vector3(0, 0, 1) * delta * flyspeed * .01)
-       if(Input.is_action_pressed("ui_down")):
+       if Input.is_action_pressed("ui_down"):
                translate(position - global_basis * Vector3(0, 0, 1) * delta * flyspeed * -.01)
-       if(Input.is_action_pressed("ui_left")):
+       if Input.is_action_pressed("ui_left"):
                translate(position - global_basis * Vector3(1, 0, 0) * delta * flyspeed * .01)
-       if(Input.is_action_pressed("ui_right")):
+       if Input.is_action_pressed("ui_right"):
                translate(position - global_basis * Vector3(1, 0, 0) * delta * flyspeed * -.01)
 
+       change_object_detected_status(ray.get_collider())
+
+
+func change_object_detected_status(ob: Object):
+       var new_object_status := false
+       if ob:  # is not null
+               new_object_status = true
+
+       if new_object_status != is_object_detected:
+               is_object_detected = new_object_status
+               emit_signal("object_detected", ob)
index 9b5bea928a7e0f17ffd714008e8ab33d36a6b778..e5d2be53c5afc47a0cbabb442c1b50289ca4e76d 100644 (file)
@@ -32,6 +32,11 @@ script = ExtResource("3_7uxif")
 mouseSensitivity = 0.2
 player = NodePath("..")
 
+[node name="RayCast3D" type="RayCast3D" parent="Camera3D"]
+transform = Transform3D(1, 0, 7.45058e-09, 0, 1, 0, -7.45058e-09, 0, 1, 0, 0, 0)
+target_position = Vector3(0, 0, -2)
+collision_mask = 2
+
 [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
 transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)
 shape = SubResource("CapsuleShape3D_22gap")