From: Eduardo Date: Sun, 11 Feb 2024 16:54:03 +0000 (+0100) Subject: cast a ray to objects on layer 2 and emit a signal when it changes X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=e9329c370cf9e24ff1ef3f032ab1ace5b9b24f1a;p=ScaryGame.git cast a ray to objects on layer 2 and emit a signal when it changes --- diff --git a/player/Camera3D.gd b/player/Camera3D.gd index e5347aa..bbafc3f 100644 --- a/player/Camera3D.gd +++ b/player/Camera3D.gd @@ -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) diff --git a/player/Player.tscn b/player/Player.tscn index 9b5bea9..e5d2be5 100644 --- a/player/Player.tscn +++ b/player/Player.tscn @@ -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")