maint: cleaned up and commented LockOnManager.cs plus some range issues
This commit is contained in:
@@ -18,8 +18,6 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
public CinemachineTargetGroup.Target cinemachineTarget;
|
public CinemachineTargetGroup.Target cinemachineTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CinemachineTargetGroup.Target playerTarget;
|
|
||||||
|
|
||||||
// Lock On settings
|
// Lock On settings
|
||||||
[Space(5)] public float lockOnRange = 40f;
|
[Space(5)] public float lockOnRange = 40f;
|
||||||
public float lockOnMaxAngle = 70f;
|
public float lockOnMaxAngle = 70f;
|
||||||
@@ -28,7 +26,6 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
|
|
||||||
// Lock On Tracking
|
// Lock On Tracking
|
||||||
[Space(10)]
|
[Space(10)]
|
||||||
public GameObject lockonGameObject; // Needed because nulling the Target below doesn't actually empty it out
|
|
||||||
|
|
||||||
[ShowInInspector]
|
[ShowInInspector]
|
||||||
ReferencedTarget mainTarget;
|
ReferencedTarget mainTarget;
|
||||||
@@ -39,8 +36,7 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
|
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
|
||||||
public CinemachineTargetGroup targetGroup;
|
public CinemachineTargetGroup targetGroup;
|
||||||
|
|
||||||
[FormerlySerializedAs("lockOnTargets")] [Space(5)]
|
private List<GameObject> acceptedTargets = new List<GameObject>();
|
||||||
public List<GameObject> acceptedTargets = new List<GameObject>();
|
|
||||||
|
|
||||||
// UI
|
// UI
|
||||||
[ShowInInspector] public UIDocument lockOnDocument;
|
[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
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||||
void Start(){
|
void Start(){
|
||||||
// Save the player target object to track later
|
// 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
|
// Quick check for things in lock-on target that aren't lock-onable
|
||||||
if (lockonGameObject != null && lockonTarget.Object.GetComponent<ILockOnTarget>() == null) {
|
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!");
|
||||||
}
|
}
|
||||||
|
|
||||||
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
||||||
@@ -64,26 +59,18 @@ 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) {
|
||||||
if (Vector3.Distance(transform.position, thisObject.transform.position) < lockOnRange) {
|
|
||||||
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
||||||
acceptedTargets.Add(thisObject);
|
acceptedTargets.Add(thisObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Update(){
|
void Update(){
|
||||||
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
|
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
|
// Iterate through targets, pushing their Target Group weight towards their goal weight, or removing them if they get too low.
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
|
|
||||||
for (int i = 0; i < activeTargets.Count; i++) {
|
for (int i = 0; i < activeTargets.Count; i++) {
|
||||||
if (activeTargets[i].gameObject == this.gameObject) {
|
if (activeTargets[i].gameObject == this.gameObject) {
|
||||||
continue;
|
continue;
|
||||||
@@ -98,38 +85,19 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
|
|
||||||
if (activeTargets[i].cinemachineTarget.Weight < 0.0001f) {
|
if (activeTargets[i].cinemachineTarget.Weight < 0.0001f) {
|
||||||
StartCoroutine(RemoveFromTargetAtFrameEnd(activeTargets[i]));
|
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){
|
IEnumerator RemoveFromTargetAtFrameEnd(ReferencedTarget target){
|
||||||
yield return new WaitForEndOfFrame();
|
yield return new WaitForEndOfFrame();
|
||||||
|
|
||||||
// if (target.targetWeight == 0) {
|
|
||||||
activeTargets.Remove(target);
|
activeTargets.Remove(target);
|
||||||
targetGroup.Targets.Remove(target.cinemachineTarget);
|
targetGroup.Targets.Remove(target.cinemachineTarget);
|
||||||
// }
|
|
||||||
|
|
||||||
// activeTargets.Remove(target);
|
|
||||||
// targetGroup.Targets.Remove(target.cinemachineTarget);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddNewTarget(GameObject targetObject, float targetWeight, bool isMain = false){
|
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) {
|
foreach (ReferencedTarget target in activeTargets) {
|
||||||
if (target.gameObject == targetObject) {
|
if (target.gameObject == targetObject) {
|
||||||
target.targetWeight = targetWeight;
|
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{
|
ReferencedTarget newTarget = new ReferencedTarget{
|
||||||
gameObject = targetObject,
|
gameObject = targetObject,
|
||||||
targetWeight = mainTargetWeight,
|
targetWeight = mainTargetWeight,
|
||||||
@@ -152,40 +121,27 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//Set as main
|
||||||
if (isMain) {
|
if (isMain) {
|
||||||
mainTarget = newTarget;
|
mainTarget = newTarget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finalize
|
||||||
activeTargets.Add(newTarget);
|
activeTargets.Add(newTarget);
|
||||||
targetGroup.Targets.Add(newTarget.cinemachineTarget);
|
targetGroup.Targets.Add(newTarget.cinemachineTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void QueueTargetRemoval(GameObject targetObject){
|
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;
|
activeTargets.Find(target => target.gameObject == targetObject).targetWeight = 0f;
|
||||||
|
|
||||||
|
// Remove as main target if it is
|
||||||
if (mainTarget == activeTargets.Find(target => target.gameObject == targetObject)) {
|
if (mainTarget == activeTargets.Find(target => target.gameObject == targetObject)) {
|
||||||
mainTarget = null;
|
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(){
|
public void ChangeLockOnTarget(){
|
||||||
Transform cameraTransform = Camera.main.transform;
|
Transform cameraTransform = Camera.main.transform;
|
||||||
|
|
||||||
@@ -195,7 +151,7 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
|
|
||||||
foreach (GameObject target in acceptedTargets) {
|
foreach (GameObject target in acceptedTargets) {
|
||||||
// Skip the current target if one exists
|
// Skip the current target if one exists
|
||||||
if (lockonGameObject != null && lockonTarget.Object.gameObject == target) {
|
if (mainTarget != null && mainTarget.gameObject == target) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,6 +163,11 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skips targets too far
|
||||||
|
if (Vector3.Distance(transform.position, target.transform.position) > lockOnRange) {
|
||||||
|
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) {
|
||||||
@@ -233,39 +194,16 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Recomment below
|
// Remove the main target that currently exists, if there is one.
|
||||||
|
|
||||||
if (mainTarget != null) {
|
if (mainTarget != null) {
|
||||||
QueueTargetRemoval(mainTarget.gameObject);
|
QueueTargetRemoval(mainTarget.gameObject);
|
||||||
// mainTarget = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new Target for the Target Group
|
// Begin tracking target, set as main
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
|
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
|
||||||
|
|
||||||
lockonGameObject = closestTarget.gameObject; // Old
|
|
||||||
// targetGroup.Targets.Add(newCineTarget); // Old
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveLockOnTarget(){
|
public void RemoveLockOnTarget(){
|
||||||
lockonTarget = null; // Old
|
|
||||||
lockonGameObject = null; // Old
|
|
||||||
|
|
||||||
QueueTargetRemoval(mainTarget.gameObject);
|
QueueTargetRemoval(mainTarget.gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user