added: early version of lock on cancel
This commit is contained in:
@@ -14,6 +14,7 @@ public class LockOnManager : MonoBehaviour{
|
||||
public UIDocument lockOnDocument;
|
||||
public GameObject lockOnTarget;
|
||||
public CinemachineTargetGroup targetGroup;
|
||||
public CinemachineOrbitalFollow cmOrbitalFollow;
|
||||
|
||||
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
|
||||
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!");
|
||||
}
|
||||
|
||||
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
||||
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
|
||||
|
||||
|
||||
// Add all nearby game objects to lock-on eligible list
|
||||
GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0);
|
||||
@@ -39,7 +39,7 @@ public class LockOnManager : MonoBehaviour{
|
||||
foreach (GameObject thisObject in allGameObjects)
|
||||
{
|
||||
if (Vector3.Distance(transform.position, thisObject.transform.position) < lockOnRange) {
|
||||
if (thisObject.GetComponent<ILockOnTarget>() != null){
|
||||
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
||||
lockOnTargets.Add(thisObject);
|
||||
}
|
||||
}
|
||||
@@ -47,11 +47,13 @@ public class LockOnManager : MonoBehaviour{
|
||||
}
|
||||
|
||||
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++) {
|
||||
if (targetGroup.Targets[i].Weight < 0.001f) {
|
||||
StartCoroutine(RemoveFromTargetAtFrameEnd(i));
|
||||
StartCoroutine(RemoveFromTargetAtFrameEnd(targetGroup.Targets[i]));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -59,9 +61,9 @@ public class LockOnManager : MonoBehaviour{
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator RemoveFromTargetAtFrameEnd(int index){
|
||||
IEnumerator RemoveFromTargetAtFrameEnd(CinemachineTargetGroup.Target indexOf){
|
||||
yield return new WaitForEndOfFrame();
|
||||
targetGroup.Targets.RemoveAt(index);
|
||||
targetGroup.Targets.Remove(indexOf);
|
||||
}
|
||||
|
||||
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
|
||||
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)) {
|
||||
if (!Physics.Raycast(Camera.main.transform.position, Camera.main.transform.position - target.transform.position)) {
|
||||
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);
|
||||
|
||||
// Set the new target to closest to screen
|
||||
@@ -88,6 +90,12 @@ public class LockOnManager : MonoBehaviour{
|
||||
|
||||
// Set the winner to the new target
|
||||
lockOnTarget = mostForwardGameObject;
|
||||
|
||||
targetGroup.Targets.Insert(1, new CinemachineTargetGroup.Target{
|
||||
Object = mostForwardGameObject.transform,
|
||||
Radius = 1f,
|
||||
Weight = 0f
|
||||
});
|
||||
} else {
|
||||
// Set it to either the next in the list or the first, if there already is a target
|
||||
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(){
|
||||
if (lockOnTarget.GetComponent<ILockOnTarget>() != null) {
|
||||
// This is just test logic to get an image above a lock on.
|
||||
// TODO: Replace with something less silly
|
||||
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
|
||||
lockOnDocument.rootVisualElement.panel,
|
||||
lockOnTarget.GetComponent<ILockOnTarget>().GetReticlePosition(),
|
||||
Camera.main.GetComponent<Camera>()
|
||||
);
|
||||
|
||||
// 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 );
|
||||
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f);
|
||||
|
||||
// Set enabled
|
||||
elementRoot.SetEnabled(true);
|
||||
if (lockOnTarget) {
|
||||
if (lockOnTarget.GetComponent<ILockOnTarget>() != null) {
|
||||
// This is just test logic to get an image above a lock on.
|
||||
// TODO: Replace with something less silly
|
||||
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
|
||||
lockOnDocument.rootVisualElement.panel,
|
||||
lockOnTarget.GetComponent<ILockOnTarget>().GetReticlePosition(),
|
||||
Camera.main.GetComponent<Camera>()
|
||||
);
|
||||
|
||||
// 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);
|
||||
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f);
|
||||
|
||||
// Set enabled
|
||||
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
|
||||
|
||||
} else {
|
||||
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
|
||||
}
|
||||
} else {
|
||||
elementRoot.SetEnabled(false);
|
||||
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +43,8 @@ public class PlayerControls : MonoBehaviour{
|
||||
public void OnLockOn(){
|
||||
GetComponent<LockOnManager>().ChangeLockOnTarget();
|
||||
}
|
||||
|
||||
public void OnCancelLockOn(){
|
||||
GetComponent<LockOnManager>().RemoveLockOnTarget();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user