improv: lock on targeting debugging option
This commit is contained in:
@@ -3,6 +3,8 @@ 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<ILockOnTarget>().SafelyDeleteTarget();
|
||||
|
||||
@@ -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<GameObject> acceptedTargets = new List<GameObject>();
|
||||
public List<GameObject> acceptedTargets = new List<GameObject>();
|
||||
|
||||
// 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<CinemachineTargetGroup>();
|
||||
@@ -74,6 +75,8 @@ public class LockOnManager : MonoBehaviour{
|
||||
GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0);
|
||||
|
||||
foreach (GameObject thisObject in allGameObjects) {
|
||||
Debug.Log($"{thisObject.name}: {thisObject.GetComponent<ILockOnTarget>() != null}");
|
||||
|
||||
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
||||
acceptedTargets.Add(thisObject);
|
||||
}
|
||||
@@ -189,32 +192,41 @@ 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<ILockOnTarget>().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<ILockOnTarget>().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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Renderer>()){
|
||||
Bounds objectBounds = gameObject.GetComponent<Renderer>().bounds;
|
||||
upValue = objectBounds.size.y;
|
||||
upValue = 4f;
|
||||
}
|
||||
|
||||
Vector3 reticlePosition = new Vector3(transform.position.x, transform.position.y + upValue, transform.position.z);
|
||||
|
||||
Reference in New Issue
Block a user