From de460d9f0adff72a3bb8bb7d8f6dc3460eb84727 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 4 Oct 2025 16:56:28 -0400 Subject: [PATCH] improv: lock on targeting debugging option --- Assets/Scripts/Core/GenericLockOnTarget.cs | 4 ++- Assets/Scripts/Core/LockOnManager.cs | 30 +++++++++++++++------- Assets/Scripts/Units/PlayerCamera.cs | 6 ++++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Core/GenericLockOnTarget.cs b/Assets/Scripts/Core/GenericLockOnTarget.cs index 2c011a0..161057e 100644 --- a/Assets/Scripts/Core/GenericLockOnTarget.cs +++ b/Assets/Scripts/Core/GenericLockOnTarget.cs @@ -3,7 +3,9 @@ using UnityEngine; public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{ public float lockonTargetRadius{ get; set; } = 1f; - + public bool lockonDebug{ get; set; } = false; + public float lockonRaycastVerticalOffset{ get; set; } + public void OnTargetDelete(){ GetComponent().SafelyDeleteTarget(); } diff --git a/Assets/Scripts/Core/LockOnManager.cs b/Assets/Scripts/Core/LockOnManager.cs index bf98e5a..c5208ba 100644 --- a/Assets/Scripts/Core/LockOnManager.cs +++ b/Assets/Scripts/Core/LockOnManager.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Numerics; +using System.Runtime.CompilerServices; using Reset; using Sirenix.OdinInspector; using Unity.Cinemachine; @@ -38,7 +39,7 @@ public class LockOnManager : MonoBehaviour{ [ReadOnly] public CinemachineTargetGroup.Target lockonTarget; public CinemachineTargetGroup targetGroup; - private List acceptedTargets = new List(); + public List acceptedTargets = new List(); // UI [ShowInInspector] public UIDocument lockOnDocument; @@ -46,13 +47,13 @@ public class LockOnManager : MonoBehaviour{ private VisualElement elementRoot; private void Awake(){ - // Register as singleton - if (Instance == null) { - Instance = this; - } else { - this.enabled = false; - return; - } + // // Register as singleton + // if (Instance == null) { + // Instance = this; + // } else { + // this.enabled = false; + // return; + // } // References from camera targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent(); @@ -74,6 +75,8 @@ public class LockOnManager : MonoBehaviour{ GameObject[] allGameObjects = GameObject.FindObjectsByType(0, 0); foreach (GameObject thisObject in allGameObjects) { + Debug.Log($"{thisObject.name}: {thisObject.GetComponent() != null}"); + if (thisObject.GetComponent() != null) { acceptedTargets.Add(thisObject); } @@ -188,33 +191,42 @@ public class LockOnManager : MonoBehaviour{ public void ChangeLockOnTarget(){ Transform cameraTransform = Camera.main.transform; + + // If there is no target, simply find the closest to the center of the camera GameObject closestTarget = null; float lowestDistanceToCenter = Mathf.Infinity; foreach (GameObject target in acceptedTargets) { + // Find out if this target wants to be debugged on it's selection process + bool debugThisTarget = target.GetComponent().lockonDebug; + // Skip the current target if one exists if (mainTarget != null && mainTarget.gameObject == target) { + if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm already the main target");} continue; } // Skip targets currently behind objects. Physics.Raycast(cameraTransform.position, - cameraTransform.position.DirectionTo(target.transform.position), out RaycastHit hit); + cameraTransform.position.DirectionTo(target.transform.position + target.GetComponent().lockonRaycastVerticalOffset * Vector3.up), out RaycastHit hit); if (hit.transform != target.transform) { + if (debugThisTarget){Debug.Log($"Not selected by {name}: Line of sight to me is blocked by {hit.collider.gameObject.name}");} continue; } // Skips targets too far if (Vector3.Distance(transform.position, target.transform.position) > lockOnRange) { + if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm too far! My distance is {Vector3.Distance(transform.position, target.transform.position)}");} continue; } // Skip targets outside lock on angle float angleFromCameraForward = Vector3.Angle(cameraTransform.forward, cameraTransform.position.DirectionTo(target.transform.position)); if (angleFromCameraForward > lockOnMaxAngle) { + if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm not forward enough in front of the camera");} continue; } diff --git a/Assets/Scripts/Units/PlayerCamera.cs b/Assets/Scripts/Units/PlayerCamera.cs index 3373107..49f0c86 100644 --- a/Assets/Scripts/Units/PlayerCamera.cs +++ b/Assets/Scripts/Units/PlayerCamera.cs @@ -2,7 +2,10 @@ using System; using UnityEngine; public interface ILockOnTarget { - public float lockonTargetRadius { set; } + public float lockonTargetRadius { set; get; } + public bool lockonDebug { set; get; } + + public float lockonRaycastVerticalOffset { set; get; } Transform transform {get;} GameObject gameObject{ get; } @@ -19,6 +22,7 @@ public interface ILockOnTarget { if (gameObject.GetComponent()){ Bounds objectBounds = gameObject.GetComponent().bounds; upValue = objectBounds.size.y; + upValue = 4f; } Vector3 reticlePosition = new Vector3(transform.position.x, transform.position.y + upValue, transform.position.z);