added: lock on switching

This commit is contained in:
Chris
2025-03-20 16:45:29 -04:00
parent 6b4ddb101f
commit 127df3d878
6 changed files with 131 additions and 7 deletions

View File

@@ -40,6 +40,15 @@
"processors": "", "processors": "",
"interactions": "", "interactions": "",
"initialStateCheck": false "initialStateCheck": false
},
{
"name": "Lock-On",
"type": "Button",
"id": "4c8aaf72-5298-4d47-b75f-90070301913b",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
} }
], ],
"bindings": [ "bindings": [
@@ -141,6 +150,28 @@
"action": "Sprint", "action": "Sprint",
"isComposite": false, "isComposite": false,
"isPartOfComposite": false "isPartOfComposite": false
},
{
"name": "",
"id": "0aec47c4-b519-4001-9408-a49abe218bd8",
"path": "",
"interactions": "",
"processors": "",
"groups": "",
"action": "Lock-On",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "c9d67b81-b640-4a64-9b31-f92c927c8345",
"path": "<Gamepad>/rightStickPress",
"interactions": "",
"processors": "",
"groups": ";Controller",
"action": "Lock-On",
"isComposite": false,
"isPartOfComposite": false
} }
] ]
} }

View File

@@ -2452,7 +2452,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!136 &263093400 --- !u!136 &263093400
CapsuleCollider: CapsuleCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -5427,7 +5427,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!136 &1505662771 --- !u!136 &1505662771
CapsuleCollider: CapsuleCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -6760,6 +6760,9 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
lockOnDocument: {fileID: 1223043171} lockOnDocument: {fileID: 1223043171}
lockOnTarget: {fileID: 1379573502} lockOnTarget: {fileID: 1379573502}
targetGroup: {fileID: 615613680}
lockOnRange: 40
lockOnTargets: []
--- !u!1 &2024357130 --- !u!1 &2024357130
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -1,11 +1,24 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Unity.Cinemachine;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using Vector2 = UnityEngine.Vector2;
using Vector3 = UnityEngine.Vector3;
public class LockOnManager : MonoBehaviour{ public class LockOnManager : MonoBehaviour{
[ShowInInspector] [ShowInInspector]
public UIDocument lockOnDocument; public UIDocument lockOnDocument;
public GameObject lockOnTarget; public GameObject lockOnTarget;
public CinemachineTargetGroup targetGroup;
public float lockOnRange;
[Space(5)]
public List<GameObject> lockOnTargets = new List<GameObject>();
private Label elementLabelName; private Label elementLabelName;
private VisualElement elementRoot; private VisualElement elementRoot;
@@ -18,9 +31,82 @@ public class LockOnManager : MonoBehaviour{
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
GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0);
foreach (GameObject thisObject in allGameObjects)
{
if (Vector3.Distance(transform.position, thisObject.transform.position) < lockOnRange) {
if (thisObject.GetComponent<ILockOnTarget>() != null){
lockOnTargets.Add(thisObject);
}
}
}
}
void Update(){
targetGroup.Targets[1].Weight = Mathf.Lerp(targetGroup.Targets[1].Weight, .2f, 5f * Time.deltaTime);
for (int i = 2; i < targetGroup.Targets.Count; i++) {
if (targetGroup.Targets[i].Weight < 0.001f) {
StartCoroutine(RemoveFromTargetAtFrameEnd(i));
continue;
}
targetGroup.Targets[i].Weight = Mathf.Lerp(targetGroup.Targets[i].Weight, 0f, 8f * Time.deltaTime);
}
}
IEnumerator RemoveFromTargetAtFrameEnd(int index){
yield return new WaitForEndOfFrame();
targetGroup.Targets.RemoveAt(index);
}
public void ChangeLockOnTarget(){
if (!lockOnTarget) {
// If there is no target, simply find the closest to the center of the camera
GameObject mostForwardGameObject = null;
float mostForwardAngle = Mathf.Infinity;
foreach (GameObject target in lockOnTargets) {
// Skip targets behind walls
if (!Physics.Raycast(Camera.main.transform.position, target.transform.position)) {
continue;
}
Vector3 dirToTarget = transform.position - target.transform.position;
float angleToTarget = Vector3.Angle(Camera.main.transform.forward, dirToTarget);
// Set the new target to closest to screen
if (angleToTarget < mostForwardAngle) {
mostForwardAngle = angleToTarget;
mostForwardGameObject = target;
}
}
// Set the winner to the new target
lockOnTarget = mostForwardGameObject;
} else {
// Set it to either the next in the list or the first, if there already is a target
int desiredIndex;
if (lockOnTargets.IndexOf(lockOnTarget) == lockOnTargets.Count - 1) {
desiredIndex = 0;
} else {
desiredIndex = lockOnTargets.IndexOf(lockOnTarget) + 1;
}
lockOnTarget = lockOnTargets[desiredIndex];
targetGroup.Targets.Insert(1, new CinemachineTargetGroup.Target{
Object = lockOnTargets[desiredIndex].transform,
Radius = 1f,
Weight = 0f
});
}
} }
// Update is called once per frame
void LateUpdate(){ void LateUpdate(){
if (lockOnTarget.GetComponent<ILockOnTarget>() != null) { if (lockOnTarget.GetComponent<ILockOnTarget>() != 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.
@@ -32,7 +118,7 @@ public class LockOnManager : MonoBehaviour{
); );
// Set name // Set name
elementLabelName.name = lockOnTarget.name; elementLabelName.text = lockOnTarget.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 - elementRoot.resolvedStyle.height * .7f ); elementRoot.style.top = new StyleLength(screenPos.y - elementRoot.resolvedStyle.height * .7f );
@@ -43,6 +129,5 @@ public class LockOnManager : MonoBehaviour{
} else { } else {
elementRoot.SetEnabled(false); elementRoot.SetEnabled(false);
} }
} }
} }

View File

@@ -1,7 +1,7 @@
using System; using System;
using UnityEngine; using UnityEngine;
public interface ILockOnTarget{ public interface ILockOnTarget {
public float lockonTargetRadius { set; } public float lockonTargetRadius { set; }
Transform transform {get;} Transform transform {get;}

View File

@@ -39,4 +39,8 @@ public class PlayerControls : MonoBehaviour{
public void OnJump(){ public void OnJump(){
graph.SendEvent<string>("InputEvent", "Jump", null); graph.SendEvent<string>("InputEvent", "Jump", null);
} }
public void OnLockOn(){
GetComponent<LockOnManager>().ChangeLockOnTarget();
}
} }

View File

@@ -7,7 +7,8 @@
"GUID:66c2eb417c67ad849907d0769db96dbf", "GUID:66c2eb417c67ad849907d0769db96dbf",
"GUID:75469ad4d38634e559750d17036d5f7c", "GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:de4e6084e6d474788bb8c799d6b461ec", "GUID:de4e6084e6d474788bb8c799d6b461ec",
"GUID:d8b63aba1907145bea998dd612889d6b" "GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:4307f53044263cf4b835bd812fc161a4"
], ],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],