changed: rebuilt lock-on system to accomodate arbitrary targets
This commit is contained in:
@@ -5,91 +5,187 @@ using System.Numerics;
|
|||||||
using Sirenix.OdinInspector;
|
using Sirenix.OdinInspector;
|
||||||
using Unity.Cinemachine;
|
using Unity.Cinemachine;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Serialization;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
using Vector2 = UnityEngine.Vector2;
|
using Vector2 = UnityEngine.Vector2;
|
||||||
using Vector3 = UnityEngine.Vector3;
|
using Vector3 = UnityEngine.Vector3;
|
||||||
|
|
||||||
public class LockOnManager : MonoBehaviour{
|
public class LockOnManager : MonoBehaviour{
|
||||||
|
class ReferencedTarget{
|
||||||
|
public GameObject gameObject;
|
||||||
|
public float targetWeight;
|
||||||
|
public float refVelocity;
|
||||||
|
public CinemachineTargetGroup.Target cinemachineTarget;
|
||||||
|
}
|
||||||
|
|
||||||
private CinemachineTargetGroup.Target playerTarget;
|
private CinemachineTargetGroup.Target playerTarget;
|
||||||
|
|
||||||
// Lock On settings
|
// Lock On settings
|
||||||
[Space(5)]
|
[Space(5)] public float lockOnRange = 40f;
|
||||||
public float lockOnRange = 40f;
|
|
||||||
public float lockOnMaxAngle = 70f;
|
public float lockOnMaxAngle = 70f;
|
||||||
|
[Range(0,1)] public float mainTargetWeight = .15f;
|
||||||
|
[FormerlySerializedAs("smoothing")] public float smoothTime = 1f;
|
||||||
|
|
||||||
// Lock On Tracking
|
// Lock On Tracking
|
||||||
[Space(10)] public GameObject lockonGameObject; // Needed because nulling the Target below doesn't actually empty it out
|
[Space(10)]
|
||||||
|
public GameObject lockonGameObject; // Needed because nulling the Target below doesn't actually empty it out
|
||||||
|
|
||||||
|
[ShowInInspector]
|
||||||
|
ReferencedTarget mainTarget;
|
||||||
|
|
||||||
|
[ShowInInspector]
|
||||||
|
List<ReferencedTarget> activeTargets = new List<ReferencedTarget>();
|
||||||
|
|
||||||
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
|
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
|
||||||
public CinemachineTargetGroup targetGroup;
|
public CinemachineTargetGroup targetGroup;
|
||||||
|
|
||||||
[Space(5)]
|
[FormerlySerializedAs("lockOnTargets")] [Space(5)]
|
||||||
public List<GameObject> lockOnTargets = new List<GameObject>();
|
public List<GameObject> acceptedTargets = new List<GameObject>();
|
||||||
|
|
||||||
// UI
|
// UI
|
||||||
[ShowInInspector]
|
[ShowInInspector] public UIDocument lockOnDocument;
|
||||||
public UIDocument lockOnDocument;
|
|
||||||
private Label elementLabelName;
|
private Label elementLabelName;
|
||||||
private VisualElement elementRoot;
|
private VisualElement elementRoot;
|
||||||
|
|
||||||
// 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];
|
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 (lockonGameObject != null && lockonTarget.Object.GetComponent<ILockOnTarget>() == null) {
|
||||||
Debug.LogError($"Game Object {lockonTarget.Object.name} does not implement the ILockOnTarget interface!");
|
Debug.LogError($"Game Object {lockonTarget.Object.name} does not implement the ILockOnTarget interface!");
|
||||||
}
|
}
|
||||||
|
|
||||||
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
||||||
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
|
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
|
||||||
|
|
||||||
// Add all nearby game objects to lock-on eligible list
|
// Add all nearby game objects to lock-on eligible list
|
||||||
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 (Vector3.Distance(transform.position, thisObject.transform.position) < lockOnRange) {
|
||||||
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
||||||
lockOnTargets.Add(thisObject);
|
acceptedTargets.Add(thisObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update(){
|
void Update(){
|
||||||
if (lockonGameObject && 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 {lockonTarget.Object.name} does not implement the ILockOnTarget interface!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the current lock-on target and increase it's weight to the .15f max slowly
|
// 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
|
// They start at 0 weight when the lock-on adds them to the group
|
||||||
if (lockonGameObject) {
|
// if (mainTarget != null) {
|
||||||
CinemachineTargetGroup.Target currentTarget = targetGroup.Targets.Find(target => target == lockonTarget);
|
// CinemachineTargetGroup.Target currentTarget = targetGroup.Targets.Find(target => target == lockonTarget);
|
||||||
currentTarget.Weight = Mathf.MoveTowards(currentTarget.Weight, .15f, .5f * Time.deltaTime);
|
// currentTarget.Weight = Mathf.MoveTowards(currentTarget.Weight, .15f, .5f * Time.deltaTime);
|
||||||
|
// }
|
||||||
|
|
||||||
|
for (int i = 0; i < activeTargets.Count; i++) {
|
||||||
|
if (activeTargets[i].gameObject == this.gameObject) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
activeTargets[i].cinemachineTarget.Weight =
|
||||||
|
Mathf.SmoothDamp(
|
||||||
|
activeTargets[i].cinemachineTarget.Weight,
|
||||||
|
activeTargets[i].targetWeight,
|
||||||
|
ref activeTargets[i].refVelocity,
|
||||||
|
smoothTime * Time.deltaTime);
|
||||||
|
|
||||||
|
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.
|
// 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++) {
|
// for (int i = 1; i < targetGroup.Targets.Count; i++) {
|
||||||
if (targetGroup.Targets[i] == lockonTarget || targetGroup.Targets[i] == playerTarget){
|
// if (targetGroup.Targets[i] == lockonTarget || targetGroup.Targets[i] == playerTarget || extraTargets.ContainsKey(targetGroup.Targets[i].Object.gameObject)){
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if (targetGroup.Targets[i].Weight < 0.001f) {
|
// if (targetGroup.Targets[i].Weight < 0.001f) {
|
||||||
StartCoroutine(RemoveFromTargetAtFrameEnd(targetGroup.Targets[i]));
|
// StartCoroutine(RemoveFromTargetAtFrameEnd(targetGroup.Targets[i]));
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
targetGroup.Targets[i].Weight = Mathf.MoveTowards(targetGroup.Targets[i].Weight, 0f, 1f * Time.deltaTime);
|
// targetGroup.Targets[i].Weight = Mathf.SmoothDamp(targetGroup.Targets[i].Weight, 0f, 1f * Time.deltaTime);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator RemoveFromTargetAtFrameEnd(CinemachineTargetGroup.Target indexOf){
|
IEnumerator RemoveFromTargetAtFrameEnd(ReferencedTarget target){
|
||||||
yield return new WaitForEndOfFrame();
|
yield return new WaitForEndOfFrame();
|
||||||
targetGroup.Targets.Remove(indexOf);
|
|
||||||
|
// if (target.targetWeight == 0) {
|
||||||
|
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){
|
||||||
|
foreach (ReferencedTarget target in activeTargets) {
|
||||||
|
if (target.gameObject == targetObject) {
|
||||||
|
target.targetWeight = targetWeight;
|
||||||
|
|
||||||
|
if (isMain) {
|
||||||
|
mainTarget = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ReferencedTarget newTarget = new ReferencedTarget{
|
||||||
|
gameObject = targetObject,
|
||||||
|
targetWeight = mainTargetWeight,
|
||||||
|
cinemachineTarget = new CinemachineTargetGroup.Target{
|
||||||
|
Object = targetObject.transform,
|
||||||
|
Radius = 1f,
|
||||||
|
Weight = 0f
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isMain) {
|
||||||
|
mainTarget = newTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
activeTargets.Add(newTarget);
|
||||||
|
targetGroup.Targets.Add(newTarget.cinemachineTarget);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void QueueTargetRemoval(GameObject targetObject){
|
||||||
|
activeTargets.Find(target => target.gameObject == targetObject).targetWeight = 0f;
|
||||||
|
|
||||||
|
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(){
|
public void ChangeLockOnTarget(){
|
||||||
Transform cameraTransform = Camera.main.transform;
|
Transform cameraTransform = Camera.main.transform;
|
||||||
|
|
||||||
@@ -97,7 +193,7 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
GameObject closestTarget = null;
|
GameObject closestTarget = null;
|
||||||
float lowestDistanceToCenter = Mathf.Infinity;
|
float lowestDistanceToCenter = Mathf.Infinity;
|
||||||
|
|
||||||
foreach (GameObject target in lockOnTargets) {
|
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 (lockonGameObject != null && lockonTarget.Object.gameObject == target) {
|
||||||
continue;
|
continue;
|
||||||
@@ -137,37 +233,54 @@ public class LockOnManager : MonoBehaviour{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Recomment below
|
||||||
|
|
||||||
|
if (mainTarget != null) {
|
||||||
|
QueueTargetRemoval(mainTarget.gameObject);
|
||||||
|
// mainTarget = null;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new Target for the Target Group
|
// Create a new Target for the Target Group
|
||||||
var newTarget = new CinemachineTargetGroup.Target{
|
var newCineTarget = new CinemachineTargetGroup.Target{
|
||||||
Object = closestTarget.transform,
|
Object = closestTarget.transform,
|
||||||
Radius = 1f,
|
Radius = 1f,
|
||||||
Weight = 0f
|
Weight = 0f
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the new target variables
|
// Set the new target variables
|
||||||
lockonTarget = newTarget;
|
lockonTarget = newCineTarget; // Old
|
||||||
lockonGameObject = closestTarget.gameObject;
|
|
||||||
|
ReferencedTarget thisTarget = new ReferencedTarget{
|
||||||
targetGroup.Targets.Add(newTarget);
|
gameObject = closestTarget.gameObject,
|
||||||
|
targetWeight = mainTargetWeight,
|
||||||
|
cinemachineTarget = newCineTarget
|
||||||
|
};
|
||||||
|
|
||||||
|
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
|
||||||
|
|
||||||
|
lockonGameObject = closestTarget.gameObject; // Old
|
||||||
|
// targetGroup.Targets.Add(newCineTarget); // Old
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveLockOnTarget(){
|
public void RemoveLockOnTarget(){
|
||||||
lockonTarget = null;
|
lockonTarget = null; // Old
|
||||||
lockonGameObject = null;
|
lockonGameObject = null; // Old
|
||||||
|
|
||||||
|
QueueTargetRemoval(mainTarget.gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LateUpdate(){
|
void LateUpdate(){
|
||||||
if (lockonGameObject) {
|
if (mainTarget != null) {
|
||||||
// This is just test logic to get an image above a lock on.
|
// This is just test logic to get an image above a lock on.
|
||||||
// TODO: Replace with something less silly
|
// TODO: Replace with something less silly
|
||||||
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
|
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
|
||||||
lockOnDocument.rootVisualElement.panel,
|
lockOnDocument.rootVisualElement.panel,
|
||||||
lockonTarget.Object.GetComponent<ILockOnTarget>().GetReticlePosition(),
|
mainTarget.gameObject.GetComponent<ILockOnTarget>().GetReticlePosition(),
|
||||||
Camera.main
|
Camera.main
|
||||||
);
|
);
|
||||||
|
|
||||||
// Set name
|
// Set name
|
||||||
elementLabelName.text = lockonTarget.Object.name;
|
elementLabelName.text = mainTarget.gameObject.name;
|
||||||
|
|
||||||
// Set position (add the width/height of the element)
|
// Set position (add the width/height of the element)
|
||||||
elementRoot.style.top = new StyleLength(screenPos.y - 25f); // Was elementRoot.resolvedStyle.height * .7f
|
elementRoot.style.top = new StyleLength(screenPos.y - 25f); // Was elementRoot.resolvedStyle.height * .7f
|
||||||
|
|||||||
Reference in New Issue
Block a user