improv: lock on targeting debugging option

This commit is contained in:
Chris
2025-10-04 16:56:28 -04:00
parent 1240afb051
commit de460d9f0a
3 changed files with 29 additions and 11 deletions

View File

@@ -3,6 +3,8 @@ using UnityEngine;
public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{ public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{
public float lockonTargetRadius{ get; set; } = 1f; public float lockonTargetRadius{ get; set; } = 1f;
public bool lockonDebug{ get; set; } = false;
public float lockonRaycastVerticalOffset{ get; set; }
public void OnTargetDelete(){ public void OnTargetDelete(){
GetComponent<ILockOnTarget>().SafelyDeleteTarget(); GetComponent<ILockOnTarget>().SafelyDeleteTarget();

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices;
using Reset; using Reset;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Unity.Cinemachine; using Unity.Cinemachine;
@@ -38,7 +39,7 @@ public class LockOnManager : MonoBehaviour{
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget; [ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
public CinemachineTargetGroup targetGroup; public CinemachineTargetGroup targetGroup;
private List<GameObject> acceptedTargets = new List<GameObject>(); public List<GameObject> acceptedTargets = new List<GameObject>();
// UI // UI
[ShowInInspector] public UIDocument lockOnDocument; [ShowInInspector] public UIDocument lockOnDocument;
@@ -46,13 +47,13 @@ public class LockOnManager : MonoBehaviour{
private VisualElement elementRoot; private VisualElement elementRoot;
private void Awake(){ private void Awake(){
// Register as singleton // // Register as singleton
if (Instance == null) { // if (Instance == null) {
Instance = this; // Instance = this;
} else { // } else {
this.enabled = false; // this.enabled = false;
return; // return;
} // }
// References from camera // References from camera
targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>(); targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
@@ -74,6 +75,8 @@ public class LockOnManager : MonoBehaviour{
GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0); GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0);
foreach (GameObject thisObject in allGameObjects) { foreach (GameObject thisObject in allGameObjects) {
Debug.Log($"{thisObject.name}: {thisObject.GetComponent<ILockOnTarget>() != null}");
if (thisObject.GetComponent<ILockOnTarget>() != null) { if (thisObject.GetComponent<ILockOnTarget>() != null) {
acceptedTargets.Add(thisObject); acceptedTargets.Add(thisObject);
} }
@@ -189,32 +192,41 @@ public class LockOnManager : MonoBehaviour{
public void ChangeLockOnTarget(){ public void ChangeLockOnTarget(){
Transform cameraTransform = Camera.main.transform; Transform cameraTransform = Camera.main.transform;
// If there is no target, simply find the closest to the center of the camera // If there is no target, simply find the closest to the center of the camera
GameObject closestTarget = null; GameObject closestTarget = null;
float lowestDistanceToCenter = Mathf.Infinity; float lowestDistanceToCenter = Mathf.Infinity;
foreach (GameObject target in acceptedTargets) { foreach (GameObject target in acceptedTargets) {
// Find out if this target wants to be debugged on it's selection process
bool debugThisTarget = target.GetComponent<ILockOnTarget>().lockonDebug;
// Skip the current target if one exists // Skip the current target if one exists
if (mainTarget != null && mainTarget.gameObject == target) { if (mainTarget != null && mainTarget.gameObject == target) {
if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm already the main target");}
continue; continue;
} }
// Skip targets currently behind objects. // Skip targets currently behind objects.
Physics.Raycast(cameraTransform.position, Physics.Raycast(cameraTransform.position,
cameraTransform.position.DirectionTo(target.transform.position), out RaycastHit hit); cameraTransform.position.DirectionTo(target.transform.position + target.GetComponent<ILockOnTarget>().lockonRaycastVerticalOffset * Vector3.up), out RaycastHit hit);
if (hit.transform != target.transform) { 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; continue;
} }
// Skips targets too far // Skips targets too far
if (Vector3.Distance(transform.position, target.transform.position) > lockOnRange) { 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; continue;
} }
// Skip targets outside lock on angle // Skip targets outside lock on angle
float angleFromCameraForward = Vector3.Angle(cameraTransform.forward, cameraTransform.position.DirectionTo(target.transform.position)); float angleFromCameraForward = Vector3.Angle(cameraTransform.forward, cameraTransform.position.DirectionTo(target.transform.position));
if (angleFromCameraForward > lockOnMaxAngle) { if (angleFromCameraForward > lockOnMaxAngle) {
if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm not forward enough in front of the camera");}
continue; continue;
} }

View File

@@ -2,7 +2,10 @@ using System;
using UnityEngine; using UnityEngine;
public interface ILockOnTarget { 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;} Transform transform {get;}
GameObject gameObject{ get; } GameObject gameObject{ get; }
@@ -19,6 +22,7 @@ public interface ILockOnTarget {
if (gameObject.GetComponent<Renderer>()){ if (gameObject.GetComponent<Renderer>()){
Bounds objectBounds = gameObject.GetComponent<Renderer>().bounds; Bounds objectBounds = gameObject.GetComponent<Renderer>().bounds;
upValue = objectBounds.size.y; upValue = objectBounds.size.y;
upValue = 4f;
} }
Vector3 reticlePosition = new Vector3(transform.position.x, transform.position.y + upValue, transform.position.z); Vector3 reticlePosition = new Vector3(transform.position.x, transform.position.y + upValue, transform.position.z);