//------------------------------------------------------------------------/ // Routines //------------------------------------------------------------------------/ bool ScanForInteractables() { Collider[] castResults = Physics.OverlapSphere(this.scanPosition, this.range); var interactions = new List <InteractionQuery>(); bool foundInteractions = false; // The scan event that will be sent var scanEvent = new DetectionEvent(); scanEvent.sensor = this; if (castResults != null) { foreach (var hit in castResults) { // Check whether it can be detected, depending on the scanmode bool detected = Detect(hit.transform); if (!detected) { continue; } // Check whether it is an interactable var interactable = hit.transform.gameObject.GetComponent <Interactable>(); if (interactable == null) { continue; } // Scan it interactable.gameObject.Dispatch <DetectionEvent>(scanEvent); // Create a query object, filling with scanned data var query = new InteractionQuery(); query.data = scanEvent.scanData; query.distance = Vector3.Distance(root.position, hit.transform.position); query.interactable = interactable; // Now store the interaction interactions.Add(query); // Note that we have scanned something foundInteractions = true; } } // Sort the interactions by the closest one interactions.Sort((a, b) => a.distance.CompareTo(b.distance)); // Save the current interactions interactivesInRange = interactions.ToArray(); // Now inform the agent of the current results var scanResult = new DetectionResultEvent(); scanResult.hasFoundInteractions = foundInteractions; this.gameObject.Dispatch <DetectionResultEvent>(scanResult); return(foundInteractions); }
private void OnTriggerEnter(Collider otherCollider) { if (!Detecting) { return; } var suspiciousObject = otherCollider.gameObject.GetComponent <SuspiciousObject>(); if (suspiciousObject == null) { return; } var guard = GetComponentInParent <Guard>(); // Set up the target and origin for the raycast. // The raycast goes from the player to the guard because that works // and the other way around it doesn't. No clue why. var origin = suspiciousObject.gameObject.transform.position; // Subtracting the origin from the target will provide the direction // to shoot the raycast in. var target = transform.parent.position - origin; // Raycast from the origin towards the target. Physics.Raycast(origin, target, out var keyCastOut); // If nothing was hit, no action is required. if (keyCastOut.transform == null) { return; } // If the developer wants to debug the raycast of the trigger, we // make a visible ray and also give a log message. if (DrawRay) { Debug.DrawRay(origin, target, RayColor, RayTime); Debug.Log($"Vision Raycast hit \"{keyCastOut.transform.gameObject.name}"); } // If something is hit, check if it's the guard. // If it is, alert the guard, if not do nothing. if (keyCastOut.transform.gameObject == guard.gameObject) { DetectionEvent?.Invoke(suspiciousObject); guard.ChangeState(suspiciousObject); } }
public static void EventHandler(string script) { var e = new DetectionEvent(script); switch (script) { case BEFORE_DO_LISTEN_DETECTION: BeforeListen(e); break; case AFTER_DO_LISTEN_DETECTION: AfterListen(e); break; case BEFORE_DO_SPOT_DETECTION: BeforeSpot(e); break; case AFTER_DO_SPOT_DETECTION: AfterSpot(e); break; } }