change: altered camera for smoother and more responsive lock-on (mainly jitter from the arbitrary target, locking rotation)

This commit is contained in:
Chris
2025-08-06 15:23:42 -04:00
parent 18a4b5228a
commit 9e8616f8da
13 changed files with 185 additions and 53 deletions

View File

@@ -11,12 +11,14 @@ using Vector2 = UnityEngine.Vector2;
using Vector3 = UnityEngine.Vector3;
public class LockOnManager : MonoBehaviour{
class ReferencedTarget{
public class ActiveLockOnTarget{
public GameObject gameObject;
public float targetWeight;
public float refVelocity;
public CinemachineTargetGroup.Target cinemachineTarget;
}
public static LockOnManager Instance;
// Lock On settings
[Space(5)] public float lockOnRange = 40f;
@@ -28,10 +30,9 @@ public class LockOnManager : MonoBehaviour{
[Space(10)]
[ShowInInspector]
ReferencedTarget mainTarget;
ActiveLockOnTarget mainTarget;
[ShowInInspector]
List<ReferencedTarget> activeTargets = new List<ReferencedTarget>();
public List<ActiveLockOnTarget> activeTargets = new List<ActiveLockOnTarget>();
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
public CinemachineTargetGroup targetGroup;
@@ -45,11 +46,19 @@ 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
// Register as singleton
if (Instance == null) {
Instance = this;
} else {
this.enabled = false;
return;
}
// Quick check for things in lock-on target that aren't lock-onable
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
Debug.LogError($"Game Object {mainTarget.gameObject.name} does not implement the ILockOnTarget interface!");
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
Debug.LogWarning($"The object <b>{mainTarget.gameObject.name}</b> has no ILockOnTarget interface. This isn't hyper critical, but adding one as a GenericLockOnTarget anyways.");
}
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
@@ -67,7 +76,8 @@ public class LockOnManager : MonoBehaviour{
void Update(){
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
Debug.LogError($"Game Object {mainTarget.gameObject.name} does not implement the ILockOnTarget interface!");
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
Debug.LogWarning($"The object <b>{mainTarget.gameObject.name}</b> has no ILockOnTarget interface. This isn't hyper critical, but adding one as a GenericLockOnTarget anyways.");
}
// Iterate through targets, pushing their Target Group weight towards their goal weight, or removing them if they get too low.
@@ -89,7 +99,7 @@ public class LockOnManager : MonoBehaviour{
}
}
IEnumerator RemoveFromTargetAtFrameEnd(ReferencedTarget target){
IEnumerator RemoveFromTargetAtFrameEnd(ActiveLockOnTarget target){
yield return new WaitForEndOfFrame();
activeTargets.Remove(target);
@@ -98,7 +108,7 @@ public class LockOnManager : MonoBehaviour{
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 (ActiveLockOnTarget target in activeTargets) {
if (target.gameObject == targetObject) {
target.targetWeight = targetWeight;
@@ -111,7 +121,7 @@ public class LockOnManager : MonoBehaviour{
}
// If it doesn't exist in the list of targets, add it
ReferencedTarget newTarget = new ReferencedTarget{
ActiveLockOnTarget newTarget = new ActiveLockOnTarget{
gameObject = targetObject,
targetWeight = mainTargetWeight,
cinemachineTarget = new CinemachineTargetGroup.Target{
@@ -131,16 +141,32 @@ public class LockOnManager : MonoBehaviour{
targetGroup.Targets.Add(newTarget.cinemachineTarget);
}
public void QueueTargetRemoval(GameObject targetObject){
public void QueueTargetRemoval(GameObject targetObject, bool deleteAfterRemoved = false){
// 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;
if (deleteAfterRemoved) {
StartCoroutine(DeleteGameObjectPostRemoval(targetObject));
}
// Remove as main target if it is
if (mainTarget == activeTargets.Find(target => target.gameObject == targetObject)) {
mainTarget = null;
}
}
IEnumerator DeleteGameObjectPostRemoval(GameObject targetObject){
ActiveLockOnTarget thisTarget = activeTargets.Find(target => target.gameObject == targetObject);
yield return new WaitForEndOfFrame();
while (activeTargets.Contains(thisTarget)) {
yield return null;
}
Destroy(thisTarget.gameObject);
}
public void ChangeLockOnTarget(){
Transform cameraTransform = Camera.main.transform;
@@ -203,7 +229,8 @@ public class LockOnManager : MonoBehaviour{
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
}
public void RemoveLockOnTarget(){
// Used by outside sources such as input to cancel lock-on.
public void RemoveMainTarget(){
QueueTargetRemoval(mainTarget.gameObject);
}