From 18a4b5228a135a6adf9bcb09eb62f5229232e656 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 5 Aug 2025 16:54:27 -0400 Subject: [PATCH] maint: cleaned up and commented LockOnManager.cs plus some range issues --- Assets/Scripts/Core/LockOnManager.cs | 116 +++++++-------------------- 1 file changed, 27 insertions(+), 89 deletions(-) diff --git a/Assets/Scripts/Core/LockOnManager.cs b/Assets/Scripts/Core/LockOnManager.cs index b565226..e94c818 100644 --- a/Assets/Scripts/Core/LockOnManager.cs +++ b/Assets/Scripts/Core/LockOnManager.cs @@ -17,9 +17,7 @@ public class LockOnManager : MonoBehaviour{ public float refVelocity; 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 acceptedTargets = new List(); + private List acceptedTargets = new List(); // 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() == null) { - Debug.LogError($"Game Object {lockonTarget.Object.name} does not implement the ILockOnTarget interface!"); + if (mainTarget != null && mainTarget.gameObject.GetComponent() == null) { + Debug.LogError($"Game Object {mainTarget.gameObject.name} does not implement the ILockOnTarget interface!"); } elementRoot = lockOnDocument.rootVisualElement.Query("LockOnGroup"); @@ -64,26 +59,18 @@ public class LockOnManager : MonoBehaviour{ GameObject[] allGameObjects = GameObject.FindObjectsByType(0, 0); foreach (GameObject thisObject in allGameObjects) { - if (Vector3.Distance(transform.position, thisObject.transform.position) < lockOnRange) { - if (thisObject.GetComponent() != null) { - acceptedTargets.Add(thisObject); - } + if (thisObject.GetComponent() != null) { + acceptedTargets.Add(thisObject); } } } void Update(){ if (mainTarget != null && mainTarget.gameObject.GetComponent() == 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; } @@ -206,7 +162,12 @@ public class LockOnManager : MonoBehaviour{ if (hit.transform != target.transform) { 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); }