added: early version of lock on cancel

This commit is contained in:
Chris
2025-07-03 14:31:59 -04:00
parent 15bf8e9b9d
commit 94c5a72049
4 changed files with 128 additions and 38 deletions

View File

@@ -49,6 +49,15 @@
"processors": "", "processors": "",
"interactions": "", "interactions": "",
"initialStateCheck": false "initialStateCheck": false
},
{
"name": "Cancel Lock-On",
"type": "Button",
"id": "f38f6ac6-41ce-416a-b47c-f948d0cde68a",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
} }
], ],
"bindings": [ "bindings": [
@@ -172,6 +181,61 @@
"action": "Lock-On", "action": "Lock-On",
"isComposite": false, "isComposite": false,
"isPartOfComposite": false "isPartOfComposite": false
},
{
"name": "",
"id": "89e90b8f-c830-4d10-9046-c1db1ef970d0",
"path": "",
"interactions": "",
"processors": "",
"groups": "",
"action": "Cancel Lock-On",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "One Modifier",
"id": "cd93e290-6f16-49c1-af36-9d3eaedeb8a2",
"path": "OneModifier",
"interactions": "",
"processors": "",
"groups": "",
"action": "Cancel Lock-On",
"isComposite": true,
"isPartOfComposite": false
},
{
"name": "modifier",
"id": "8713a052-f2c4-439a-acae-f845a5654819",
"path": "",
"interactions": "",
"processors": "",
"groups": "",
"action": "Cancel Lock-On",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "binding",
"id": "013863ac-050d-4d7f-a36c-bce06827ad17",
"path": "",
"interactions": "",
"processors": "",
"groups": "",
"action": "Cancel Lock-On",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "",
"id": "84c3f849-8d7f-40ea-8c6c-4b5af0d9addd",
"path": "<Gamepad>/rightStickPress",
"interactions": "Hold",
"processors": "",
"groups": ";Controller",
"action": "Cancel Lock-On",
"isComposite": false,
"isPartOfComposite": false
} }
] ]
} }

View File

@@ -2644,7 +2644,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 330585543} m_GameObject: {fileID: 330585543}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: 0.015995976, y: -0.067584075, z: 0.0010836906, w: 0.99758476} m_LocalRotation: {x: 0.09176971, y: 0.050800513, z: -0.0046878606, w: 0.99447256}
m_LocalPosition: {x: 1.5039132, y: 2.1421127, z: -6.8609486} m_LocalPosition: {x: 1.5039132, y: 2.1421127, z: -6.8609486}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
@@ -2898,7 +2898,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 402424904} m_GameObject: {fileID: 402424904}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: 0.015995974, y: -0.06758407, z: 0.0010836904, w: 0.9975848} m_LocalRotation: {x: 0.09176965, y: 0.050800513, z: -0.0046878555, w: 0.9944726}
m_LocalPosition: {x: 1.5039132, y: 2.1421127, z: -6.8609486} m_LocalPosition: {x: 1.5039132, y: 2.1421127, z: -6.8609486}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
@@ -2945,8 +2945,8 @@ MonoBehaviour:
Wrap: 1 Wrap: 1
Recentering: Recentering:
Enabled: 0 Enabled: 0
Wait: 1 Wait: 0
Time: 2 Time: 0.3
Restrictions: 0 Restrictions: 0
VerticalAxis: VerticalAxis:
Value: 20 Value: 20
@@ -3398,9 +3398,6 @@ MonoBehaviour:
- Object: {fileID: 1992890473} - Object: {fileID: 1992890473}
Weight: 1 Weight: 1
Radius: 3.64 Radius: 3.64
- Object: {fileID: 1379573506}
Weight: 0.15
Radius: 3.64
m_LegacyTargets: [] m_LegacyTargets: []
--- !u!4 &615613681 --- !u!4 &615613681
Transform: Transform:
@@ -3411,7 +3408,7 @@ Transform:
m_GameObject: {fileID: 615613679} m_GameObject: {fileID: 615613679}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 1.5039132, y: -0.81913054, z: -1.296337}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
@@ -6759,8 +6756,9 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
lockOnDocument: {fileID: 1223043171} lockOnDocument: {fileID: 1223043171}
lockOnTarget: {fileID: 1379573502} lockOnTarget: {fileID: 0}
targetGroup: {fileID: 615613680} targetGroup: {fileID: 615613680}
cmOrbitalFollow: {fileID: 0}
lockOnRange: 40 lockOnRange: 40
lockOnTargets: [] lockOnTargets: []
--- !u!1 &2024357130 --- !u!1 &2024357130

View File

@@ -14,6 +14,7 @@ public class LockOnManager : MonoBehaviour{
public UIDocument lockOnDocument; public UIDocument lockOnDocument;
public GameObject lockOnTarget; public GameObject lockOnTarget;
public CinemachineTargetGroup targetGroup; public CinemachineTargetGroup targetGroup;
public CinemachineOrbitalFollow cmOrbitalFollow;
public float lockOnRange; public float lockOnRange;
@@ -25,13 +26,12 @@ 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()
{ {
if (lockOnTarget.GetComponent<ILockOnTarget>() == null) { if (lockOnTarget && lockOnTarget.GetComponent<ILockOnTarget>() == null) {
Debug.LogError($"Game Object {lockOnTarget.name} does not implement the ILockOnTarget interface. Not processing lock-on actions!"); Debug.LogError($"Game Object {lockOnTarget.name} does not implement the ILockOnTarget interface. Not processing lock-on actions!");
} }
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);
@@ -39,7 +39,7 @@ public class LockOnManager : MonoBehaviour{
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); lockOnTargets.Add(thisObject);
} }
} }
@@ -47,11 +47,13 @@ public class LockOnManager : MonoBehaviour{
} }
void Update(){ void Update(){
targetGroup.Targets[1].Weight = Mathf.Lerp(targetGroup.Targets[1].Weight, .2f, 5f * Time.deltaTime); if (targetGroup.Targets.Count > 1){
targetGroup.Targets[1].Weight = Mathf.Lerp(targetGroup.Targets[1].Weight, .15f, 5f * Time.deltaTime);
}
for (int i = 2; i < targetGroup.Targets.Count; i++) { for (int i = 2; i < targetGroup.Targets.Count; i++) {
if (targetGroup.Targets[i].Weight < 0.001f) { if (targetGroup.Targets[i].Weight < 0.001f) {
StartCoroutine(RemoveFromTargetAtFrameEnd(i)); StartCoroutine(RemoveFromTargetAtFrameEnd(targetGroup.Targets[i]));
continue; continue;
} }
@@ -59,9 +61,9 @@ public class LockOnManager : MonoBehaviour{
} }
} }
IEnumerator RemoveFromTargetAtFrameEnd(int index){ IEnumerator RemoveFromTargetAtFrameEnd(CinemachineTargetGroup.Target indexOf){
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
targetGroup.Targets.RemoveAt(index); targetGroup.Targets.Remove(indexOf);
} }
public void ChangeLockOnTarget(){ public void ChangeLockOnTarget(){
@@ -69,14 +71,14 @@ public class LockOnManager : MonoBehaviour{
// If there is no target, simply find the closest to the center of the camera // If there is no target, simply find the closest to the center of the camera
GameObject mostForwardGameObject = null; GameObject mostForwardGameObject = null;
float mostForwardAngle = Mathf.Infinity; float mostForwardAngle = Mathf.Infinity;
foreach (GameObject target in lockOnTargets) { foreach (GameObject target in lockOnTargets) {
// Skip targets behind walls // Skip targets behind walls
if (!Physics.Raycast(Camera.main.transform.position, target.transform.position)) { if (!Physics.Raycast(Camera.main.transform.position, Camera.main.transform.position - target.transform.position)) {
continue; continue;
} }
Vector3 dirToTarget = transform.position - target.transform.position; Vector3 dirToTarget = target.transform.position - Camera.main.transform.position;
float angleToTarget = Vector3.Angle(Camera.main.transform.forward, dirToTarget); float angleToTarget = Vector3.Angle(Camera.main.transform.forward, dirToTarget);
// Set the new target to closest to screen // Set the new target to closest to screen
@@ -88,6 +90,12 @@ public class LockOnManager : MonoBehaviour{
// Set the winner to the new target // Set the winner to the new target
lockOnTarget = mostForwardGameObject; lockOnTarget = mostForwardGameObject;
targetGroup.Targets.Insert(1, new CinemachineTargetGroup.Target{
Object = mostForwardGameObject.transform,
Radius = 1f,
Weight = 0f
});
} else { } else {
// Set it to either the next in the list or the first, if there already is a target // Set it to either the next in the list or the first, if there already is a target
int desiredIndex; int desiredIndex;
@@ -107,27 +115,43 @@ public class LockOnManager : MonoBehaviour{
} }
} }
public void RemoveLockOnTarget(){
lockOnTarget = null;
cmOrbitalFollow.HorizontalAxis.Center = transform.rotation.eulerAngles.y;
cmOrbitalFollow.HorizontalAxis.TriggerRecentering();
}
void UpdateLockOnUI(){
}
void LateUpdate(){ void LateUpdate(){
if (lockOnTarget.GetComponent<ILockOnTarget>() != null) { if (lockOnTarget) {
// This is just test logic to get an image above a lock on. if (lockOnTarget.GetComponent<ILockOnTarget>() != null) {
// TODO: Replace with something less silly // This is just test logic to get an image above a lock on.
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel( // TODO: Replace with something less silly
lockOnDocument.rootVisualElement.panel, Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
lockOnTarget.GetComponent<ILockOnTarget>().GetReticlePosition(), lockOnDocument.rootVisualElement.panel,
Camera.main.GetComponent<Camera>() lockOnTarget.GetComponent<ILockOnTarget>().GetReticlePosition(),
); Camera.main.GetComponent<Camera>()
);
// Set name
elementLabelName.text = lockOnTarget.name; // Set name
elementLabelName.text = lockOnTarget.name;
// Set position (add the width/height of the element)
elementRoot.style.top = new StyleLength(screenPos.y - elementRoot.resolvedStyle.height * .7f ); // Set position (add the width/height of the element)
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f); elementRoot.style.top = new StyleLength(screenPos.y - elementRoot.resolvedStyle.height * .7f);
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f);
// Set enabled
elementRoot.SetEnabled(true); // Set enabled
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
} else {
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
}
} else { } else {
elementRoot.SetEnabled(false); elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
} }
} }
} }

View File

@@ -43,4 +43,8 @@ public class PlayerControls : MonoBehaviour{
public void OnLockOn(){ public void OnLockOn(){
GetComponent<LockOnManager>().ChangeLockOnTarget(); GetComponent<LockOnManager>().ChangeLockOnTarget();
} }
public void OnCancelLockOn(){
GetComponent<LockOnManager>().RemoveLockOnTarget();
}
} }