maint: cleaned up and commented LockOnManager.cs plus some range issues

This commit is contained in:
Chris
2025-08-05 16:54:27 -04:00
parent a57757c63e
commit 18a4b5228a

View File

@@ -18,8 +18,6 @@ public class LockOnManager : MonoBehaviour{
public CinemachineTargetGroup.Target cinemachineTarget;
}
private CinemachineTargetGroup.Target playerTarget;
// Lock On settings
[Space(5)] public float lockOnRange = 40f;
public float lockOnMaxAngle = 70f;
@@ -28,7 +26,6 @@ public class LockOnManager : MonoBehaviour{
// Lock On Tracking
[Space(10)]
public GameObject lockonGameObject; // Needed because nulling the Target below doesn't actually empty it out
[ShowInInspector]
ReferencedTarget mainTarget;
@@ -39,8 +36,7 @@ public class LockOnManager : MonoBehaviour{
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
public CinemachineTargetGroup targetGroup;
[FormerlySerializedAs("lockOnTargets")] [Space(5)]
public List<GameObject> acceptedTargets = new List<GameObject>();
private List<GameObject> acceptedTargets = new List<GameObject>();
// UI
[ShowInInspector] public UIDocument lockOnDocument;
@@ -50,11 +46,10 @@ public class LockOnManager : MonoBehaviour{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start(){
// Save the player target object to track later
playerTarget = targetGroup.Targets[0];
// Quick check for things in lock-on target that aren't lock-onable
if (lockonGameObject != null && lockonTarget.Object.GetComponent<ILockOnTarget>() == null) {
Debug.LogError($"Game Object {lockonTarget.Object.name} does not implement the ILockOnTarget interface!");
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
Debug.LogError($"Game Object {mainTarget.gameObject.name} does not implement the ILockOnTarget interface!");
}
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
@@ -64,26 +59,18 @@ public class LockOnManager : MonoBehaviour{
GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0);
foreach (GameObject thisObject in allGameObjects) {
if (Vector3.Distance(transform.position, thisObject.transform.position) < lockOnRange) {
if (thisObject.GetComponent<ILockOnTarget>() != null) {
acceptedTargets.Add(thisObject);
}
if (thisObject.GetComponent<ILockOnTarget>() != null) {
acceptedTargets.Add(thisObject);
}
}
}
void Update(){
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
Debug.LogError($"Game Object {lockonTarget.Object.name} does not implement the ILockOnTarget interface!");
Debug.LogError($"Game Object {mainTarget.gameObject.name} does not implement the ILockOnTarget interface!");
}
// Find the current lock-on target and increase it's weight to the .15f max slowly
// They start at 0 weight when the lock-on adds them to the group
// if (mainTarget != null) {
// CinemachineTargetGroup.Target currentTarget = targetGroup.Targets.Find(target => target == lockonTarget);
// currentTarget.Weight = Mathf.MoveTowards(currentTarget.Weight, .15f, .5f * Time.deltaTime);
// }
// Iterate through targets, pushing their Target Group weight towards their goal weight, or removing them if they get too low.
for (int i = 0; i < activeTargets.Count; i++) {
if (activeTargets[i].gameObject == this.gameObject) {
continue;
@@ -98,38 +85,19 @@ public class LockOnManager : MonoBehaviour{
if (activeTargets[i].cinemachineTarget.Weight < 0.0001f) {
StartCoroutine(RemoveFromTargetAtFrameEnd(activeTargets[i]));
continue;
}
}
// If a target is not the current lock on target, lower their targeting weight. When low enough to not cause a sharp jitter, remove them.
// for (int i = 1; i < targetGroup.Targets.Count; i++) {
// if (targetGroup.Targets[i] == lockonTarget || targetGroup.Targets[i] == playerTarget || extraTargets.ContainsKey(targetGroup.Targets[i].Object.gameObject)){
// continue;
// }
//
// if (targetGroup.Targets[i].Weight < 0.001f) {
// StartCoroutine(RemoveFromTargetAtFrameEnd(targetGroup.Targets[i]));
// continue;
// }
//
// targetGroup.Targets[i].Weight = Mathf.SmoothDamp(targetGroup.Targets[i].Weight, 0f, 1f * Time.deltaTime);
// }
}
IEnumerator RemoveFromTargetAtFrameEnd(ReferencedTarget target){
yield return new WaitForEndOfFrame();
// if (target.targetWeight == 0) {
activeTargets.Remove(target);
targetGroup.Targets.Remove(target.cinemachineTarget);
// }
// activeTargets.Remove(target);
// targetGroup.Targets.Remove(target.cinemachineTarget);
activeTargets.Remove(target);
targetGroup.Targets.Remove(target.cinemachineTarget);
}
public void AddNewTarget(GameObject targetObject, float targetWeight, bool isMain = false){
// Check that the target doesn't already exist- if it does, just change it's weight/make it main
foreach (ReferencedTarget target in activeTargets) {
if (target.gameObject == targetObject) {
target.targetWeight = targetWeight;
@@ -142,6 +110,7 @@ public class LockOnManager : MonoBehaviour{
}
}
// If it doesn't exist in the list of targets, add it
ReferencedTarget newTarget = new ReferencedTarget{
gameObject = targetObject,
targetWeight = mainTargetWeight,
@@ -152,40 +121,27 @@ public class LockOnManager : MonoBehaviour{
}
};
//Set as main
if (isMain) {
mainTarget = newTarget;
}
// Finalize
activeTargets.Add(newTarget);
targetGroup.Targets.Add(newTarget.cinemachineTarget);
}
public void QueueTargetRemoval(GameObject targetObject){
// Ostensibly removes targest by setting their target weight to 0. Update loop finds targets with no weight and reduces their impact on the camera
// After it smooths their current weight to 0, it removes them
activeTargets.Find(target => target.gameObject == targetObject).targetWeight = 0f;
// Remove as main target if it is
if (mainTarget == activeTargets.Find(target => target.gameObject == targetObject)) {
mainTarget = null;
}
Debug.Log(activeTargets.Find(target => target.gameObject == targetObject).targetWeight);
}
// public void AddToExtraTargets(GameObject target, float weight){
// if (extraTargets.ContainsKey(target)) {
// return;
// }
//
// extraTargets.Add(target, weight);
//
// targetGroup.Targets.Add(new CinemachineTargetGroup.Target(){ Object = target.transform, Weight = 0f });
// }
//
// public void RemoveFromExtraTargets(GameObject target){
// if (extraTargets.ContainsKey(target)) {
// extraTargets.Remove(target);
// }
// }
public void ChangeLockOnTarget(){
Transform cameraTransform = Camera.main.transform;
@@ -195,7 +151,7 @@ public class LockOnManager : MonoBehaviour{
foreach (GameObject target in acceptedTargets) {
// Skip the current target if one exists
if (lockonGameObject != null && lockonTarget.Object.gameObject == target) {
if (mainTarget != null && mainTarget.gameObject == target) {
continue;
}
@@ -207,6 +163,11 @@ public class LockOnManager : MonoBehaviour{
continue;
}
// Skips targets too far
if (Vector3.Distance(transform.position, target.transform.position) > lockOnRange) {
continue;
}
// Skip targets outside lock on angle
float angleFromCameraForward = Vector3.Angle(cameraTransform.forward, cameraTransform.position.DirectionTo(target.transform.position));
if (angleFromCameraForward > lockOnMaxAngle) {
@@ -233,39 +194,16 @@ public class LockOnManager : MonoBehaviour{
return;
}
// TODO: Recomment below
// Remove the main target that currently exists, if there is one.
if (mainTarget != null) {
QueueTargetRemoval(mainTarget.gameObject);
// mainTarget = null;
}
// Create a new Target for the Target Group
var newCineTarget = new CinemachineTargetGroup.Target{
Object = closestTarget.transform,
Radius = 1f,
Weight = 0f
};
// Set the new target variables
lockonTarget = newCineTarget; // Old
ReferencedTarget thisTarget = new ReferencedTarget{
gameObject = closestTarget.gameObject,
targetWeight = mainTargetWeight,
cinemachineTarget = newCineTarget
};
// Begin tracking target, set as main
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
lockonGameObject = closestTarget.gameObject; // Old
// targetGroup.Targets.Add(newCineTarget); // Old
}
public void RemoveLockOnTarget(){
lockonTarget = null; // Old
lockonGameObject = null; // Old
QueueTargetRemoval(mainTarget.gameObject);
}