feat: lock-on added, rotation reworked
This commit is contained in:
@@ -2,7 +2,6 @@ using UnityEngine;
|
||||
|
||||
public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{
|
||||
public float lockonTargetRadius{ get; set; } = 1f;
|
||||
public Renderer thisRenderer { get; }
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start(){
|
||||
@@ -10,6 +9,6 @@ public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{
|
||||
}
|
||||
|
||||
void Update(){
|
||||
Debug.Log((this as ILockOnTarget).GetReticlePosition());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using NodeCanvas.Framework;
|
||||
using ParadoxNotion.Design;
|
||||
using ParadoxNotion.Services;
|
||||
using Unity.Cinemachine;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
public enum PlayerFacingDirection{
|
||||
Target = 0,
|
||||
Movement,
|
||||
MatchCamera
|
||||
}
|
||||
|
||||
namespace NodeCanvas.Tasks.Actions {
|
||||
|
||||
[Category("Reset/Movement")]
|
||||
@@ -18,19 +27,36 @@ namespace NodeCanvas.Tasks.Actions {
|
||||
public BBParameter<float> jumpPower;
|
||||
public BBParameter<float> jumpPowerDecay;
|
||||
|
||||
// Rotation
|
||||
public PlayerFacingDirection playerFacingDirection;
|
||||
|
||||
[ShowIf("playerFacingDirection", 0)]
|
||||
public Vector3 rotationTargetPosition;
|
||||
|
||||
public BBParameter<float> rotationSpeed = 5f;
|
||||
public BBParameter<float> rotationSmoothing;
|
||||
|
||||
private float currentRotSpeed;
|
||||
private Vector3 rotationNoY;
|
||||
private float lastLookMagnitude;
|
||||
private Quaternion targetRotation;
|
||||
|
||||
// References
|
||||
private PlayerControls controls;
|
||||
|
||||
public BBParameter<float> gravityPower;
|
||||
|
||||
//Use for initialization. This is called only once in the lifetime of the task.
|
||||
//Return null if init was successfull. Return an error string otherwise
|
||||
protected override string OnInit() {
|
||||
// Append the late update method to actually happen on late update
|
||||
MonoManager.current.onLateUpdate += LateUpdate;
|
||||
|
||||
// What
|
||||
currentRotSpeed = rotationSpeed.value;
|
||||
|
||||
// Reference to controls
|
||||
controls = agent.GetComponent<PlayerControls>();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -43,67 +69,63 @@ namespace NodeCanvas.Tasks.Actions {
|
||||
|
||||
//Called once per frame while the action is active.
|
||||
protected override void OnUpdate(){
|
||||
// Get input values
|
||||
Vector2 rawLookInputVector3 = agent.GetComponent<PlayerControls>().rawLookInput;
|
||||
Vector2 rawMoveInputVector3 = agent.GetComponent<PlayerControls>().rawMoveInput;
|
||||
|
||||
// Accelerate gravity
|
||||
gravityPower.value += gravityAcceleration.value * Time.deltaTime;
|
||||
gravityPower.value = Mathf.Min(gravityPower.value, gravityMax.value);
|
||||
|
||||
// Calculate rotation speed
|
||||
currentRotSpeed = Mathf.Lerp(currentRotSpeed, rotationSpeed.value, rotationSmoothing.value * Time.deltaTime);
|
||||
|
||||
// Process input rotation
|
||||
agent.transform.Rotate(new Vector3(0, agent.GetComponent<PlayerControls>().rawLookInput.x * rotationSpeed.value * 360f * Time.deltaTime)); // *360 to account for a full circle taking a long ass time
|
||||
|
||||
// Process movement rotation
|
||||
if (agent.isGrounded){
|
||||
// Quaternion cameraRotNoY = Quaternion.Euler(0f, Camera.main.transform.rotation.eulerAngles.y , 0f);
|
||||
// agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, Quaternion.LookRotation(agent.transform.position + groundMoveDirection.value), 10f * Time.deltaTime);
|
||||
} else {
|
||||
// rotationNoY = airMoveDirection.value;
|
||||
// rotationNoY.x = 0f;
|
||||
// rotationNoY.z = 0f;
|
||||
// agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, Quaternion.Euler(rotationNoY), 5f * Time.deltaTime);
|
||||
switch (playerFacingDirection) {
|
||||
case PlayerFacingDirection.Target:
|
||||
// Set rotation to just the direction of the target
|
||||
targetRotation = Quaternion.LookRotation((agent.transform.position.DirectionTo(rotationTargetPosition)).Flatten(null, 0, null));
|
||||
|
||||
break;
|
||||
case PlayerFacingDirection.Movement:
|
||||
// Check magnitude to avoid the "Look rotation viewing vector is zero" debug
|
||||
if (controls.rawMoveInput.magnitude == 0) { break; }
|
||||
|
||||
// Set desired rotation to input direction, with respect to camera rotation
|
||||
targetRotation = Quaternion.LookRotation(new Vector3(controls.rawMoveInput.x, 0, controls.rawMoveInput.y)) *
|
||||
Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0f, null, 0f));
|
||||
break;
|
||||
case PlayerFacingDirection.MatchCamera:
|
||||
// Craft a new rotation that flattens the camera's rotation to the ground
|
||||
// Needed to keep the character from inheriting the camera's height-rotation
|
||||
targetRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0));
|
||||
break;
|
||||
}
|
||||
|
||||
// Set final rotation
|
||||
agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, targetRotation, 10f * Time.deltaTime);
|
||||
|
||||
// Construct move direction
|
||||
Vector3 finalMoveDir = Vector3.zero;
|
||||
Vector3 gravityMoveDirection;
|
||||
|
||||
// Create movement direction for ground movement based on move direction, facing direction, and input
|
||||
// Start by lerping facing direction towards move direction based on input power
|
||||
rotationNoY = airMoveDirection.value;
|
||||
rotationNoY.x = 0f;
|
||||
rotationNoY.z = 0f;
|
||||
|
||||
// agent.transform.rotation = Quaternion.RotateTowards(agent.transform.rotation, Quaternion.Euler(rotationNoY), )
|
||||
|
||||
// Vector3 groundMoveDirModified = Vector3.Lerp();
|
||||
|
||||
// Add input movement
|
||||
if (agent.isGrounded){
|
||||
Quaternion cameraRotNoY = Quaternion.Euler(0f, Camera.main.transform.rotation.eulerAngles.y , 0f);
|
||||
|
||||
finalMoveDir += groundMoveDirection.value + Vector3.down * .03f;
|
||||
gravityMoveDirection = new(0f, jumpPower.value + (Physics.gravity.y *.08f), 0f);
|
||||
gravityMoveDirection = new(0f, jumpPower.value + (Physics.gravity.y *.3f), 0f);
|
||||
} else {
|
||||
finalMoveDir += airMoveDirection.value;
|
||||
gravityMoveDirection = new(0f, jumpPower.value + Physics.gravity.y * gravityPower.value, 0f);
|
||||
}
|
||||
|
||||
// Do final movement
|
||||
if (agent.isGrounded) {
|
||||
agent.Move((Camera.main.transform.rotation * finalMoveDir + gravityMoveDirection) * Time.deltaTime);
|
||||
} else {
|
||||
agent.Move((finalMoveDir + gravityMoveDirection) * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Reset gravity power when grounded
|
||||
if (agent.isGrounded) {
|
||||
gravityPower.value = 0f;
|
||||
}
|
||||
|
||||
// Do final movement
|
||||
if (agent.isGrounded) {
|
||||
agent.Move((Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0f, null, 0f)) * finalMoveDir + gravityMoveDirection) * Time.deltaTime);
|
||||
} else {
|
||||
agent.Move((finalMoveDir + gravityMoveDirection) * Time.deltaTime);
|
||||
}
|
||||
|
||||
// ???? Moved this above but don't remember if this needs to be here still
|
||||
if (agent.isGrounded) {
|
||||
jumpPower.value = 0f;
|
||||
}
|
||||
@@ -111,7 +133,6 @@ namespace NodeCanvas.Tasks.Actions {
|
||||
|
||||
// Gravity is processed in LateUpdate (added to MonoManager's onLateUpdate in OnInit())
|
||||
public void LateUpdate(){
|
||||
|
||||
// Decay jump power
|
||||
jumpPower.value = Mathf.Lerp(jumpPower.value, 0f, jumpPowerDecay.value * Time.deltaTime);
|
||||
}
|
||||
|
||||
@@ -10,24 +10,36 @@ using Vector2 = UnityEngine.Vector2;
|
||||
using Vector3 = UnityEngine.Vector3;
|
||||
|
||||
public class LockOnManager : MonoBehaviour{
|
||||
[ShowInInspector]
|
||||
public UIDocument lockOnDocument;
|
||||
public GameObject lockOnTarget;
|
||||
|
||||
private CinemachineTargetGroup.Target playerTarget;
|
||||
|
||||
// Lock On settings
|
||||
[Space(5)]
|
||||
public float lockOnRange = 40f;
|
||||
public float lockOnMaxAngle = 70f;
|
||||
|
||||
// Lock On Tracking
|
||||
[Space(10)] public GameObject lockonGameObject; // Needed because nulling the Target below doesn't actually empty it out
|
||||
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
|
||||
public CinemachineTargetGroup targetGroup;
|
||||
public CinemachineOrbitalFollow cmOrbitalFollow;
|
||||
|
||||
public float lockOnRange;
|
||||
|
||||
|
||||
[Space(5)]
|
||||
public List<GameObject> lockOnTargets = new List<GameObject>();
|
||||
|
||||
// UI
|
||||
[ShowInInspector]
|
||||
public UIDocument lockOnDocument;
|
||||
private Label elementLabelName;
|
||||
private VisualElement elementRoot;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
if (lockOnTarget && lockOnTarget.GetComponent<ILockOnTarget>() == null) {
|
||||
Debug.LogError($"Game Object {lockOnTarget.name} does not implement the ILockOnTarget interface. Not processing lock-on actions!");
|
||||
void Start(){
|
||||
// Save the player target object to track later
|
||||
playerTarget = targetGroup.Targets[0];
|
||||
|
||||
// Quick check for things in lock-on target that aren't lock-onable
|
||||
if (lockonGameObject != null && lockonTarget.Object.GetComponent<ILockOnTarget>() == null) {
|
||||
Debug.LogError($"Game Object {lockonTarget.Object.name} does not implement the ILockOnTarget interface!");
|
||||
}
|
||||
|
||||
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
||||
@@ -47,17 +59,29 @@ public class LockOnManager : MonoBehaviour{
|
||||
}
|
||||
|
||||
void Update(){
|
||||
if (targetGroup.Targets.Count > 1){
|
||||
targetGroup.Targets[1].Weight = Mathf.Lerp(targetGroup.Targets[1].Weight, .15f, 5f * Time.deltaTime);
|
||||
if (lockonGameObject && lockonTarget.Object.GetComponent<ILockOnTarget>() == null) {
|
||||
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
|
||||
// They start at 0 weight when the lock-on adds them to the group
|
||||
if (lockonGameObject) {
|
||||
CinemachineTargetGroup.Target currentTarget = targetGroup.Targets.Find(target => target == lockonTarget);
|
||||
currentTarget.Weight = Mathf.MoveTowards(currentTarget.Weight, .15f, .5f * Time.deltaTime);
|
||||
}
|
||||
|
||||
for (int i = 2; i < targetGroup.Targets.Count; i++) {
|
||||
// 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++) {
|
||||
if (targetGroup.Targets[i] == lockonTarget || targetGroup.Targets[i] == playerTarget){
|
||||
continue;
|
||||
}
|
||||
|
||||
if (targetGroup.Targets[i].Weight < 0.001f) {
|
||||
StartCoroutine(RemoveFromTargetAtFrameEnd(targetGroup.Targets[i]));
|
||||
continue;
|
||||
}
|
||||
|
||||
targetGroup.Targets[i].Weight = Mathf.Lerp(targetGroup.Targets[i].Weight, 0f, 8f * Time.deltaTime);
|
||||
targetGroup.Targets[i].Weight = Mathf.MoveTowards(targetGroup.Targets[i].Weight, 0f, 1f * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,91 +91,92 @@ public class LockOnManager : MonoBehaviour{
|
||||
}
|
||||
|
||||
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, Camera.main.transform.position - target.transform.position)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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
|
||||
if (angleToTarget < mostForwardAngle) {
|
||||
mostForwardAngle = angleToTarget;
|
||||
mostForwardGameObject = target;
|
||||
}
|
||||
Transform cameraTransform = Camera.main.transform;
|
||||
|
||||
// If there is no target, simply find the closest to the center of the camera
|
||||
GameObject closestTarget = null;
|
||||
float lowestDistanceToCenter = Mathf.Infinity;
|
||||
|
||||
foreach (GameObject target in lockOnTargets) {
|
||||
// Skip the current target if one exists
|
||||
if (lockonGameObject != null && lockonTarget.Object.gameObject == target) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
if (lockOnTargets.IndexOf(lockOnTarget) == lockOnTargets.Count - 1) {
|
||||
desiredIndex = 0;
|
||||
} else {
|
||||
desiredIndex = lockOnTargets.IndexOf(lockOnTarget) + 1;
|
||||
// Skip targets currently behind objects.
|
||||
Physics.Raycast(cameraTransform.position,
|
||||
cameraTransform.position.DirectionTo(target.transform.position), out RaycastHit hit);
|
||||
|
||||
if (hit.transform != target.transform) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip targets outside lock on angle
|
||||
float angleFromCameraForward = Vector3.Angle(cameraTransform.forward, cameraTransform.position.DirectionTo(target.transform.position));
|
||||
if (angleFromCameraForward > lockOnMaxAngle) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find how close this target is from the center of the screen
|
||||
Vector3 targetScreenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
|
||||
float distanceFromScreenCenter = targetScreenPoint.Flatten(null, null, 0f).magnitude - new Vector3(Screen.width, Screen.height, 0f).magnitude / 2f;
|
||||
distanceFromScreenCenter = Mathf.Abs(distanceFromScreenCenter);
|
||||
|
||||
// Debug.Log($"{target.name}: {distanceFromScreenCenter} pixels, {angleFromCameraForward} degrees");
|
||||
|
||||
// Set the new target to closest to screen
|
||||
if (distanceFromScreenCenter < lowestDistanceToCenter) {
|
||||
lowestDistanceToCenter = distanceFromScreenCenter;
|
||||
closestTarget = target;
|
||||
}
|
||||
|
||||
lockOnTarget = lockOnTargets[desiredIndex];
|
||||
targetGroup.Targets.Insert(1, new CinemachineTargetGroup.Target{
|
||||
Object = lockOnTargets[desiredIndex].transform,
|
||||
Radius = 1f,
|
||||
Weight = 0f
|
||||
});
|
||||
}
|
||||
|
||||
// Catch exception from nothing being found
|
||||
if (!closestTarget) {
|
||||
Debug.LogWarning("Lock-on attempted, but no lock on target was found viable.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a new Target for the Target Group
|
||||
var newTarget = new CinemachineTargetGroup.Target{
|
||||
Object = closestTarget.transform,
|
||||
Radius = 1f,
|
||||
Weight = 0f
|
||||
};
|
||||
|
||||
// Set the new target variables
|
||||
lockonTarget = newTarget;
|
||||
lockonGameObject = closestTarget.gameObject;
|
||||
|
||||
targetGroup.Targets.Add(newTarget);
|
||||
}
|
||||
|
||||
public void RemoveLockOnTarget(){
|
||||
lockOnTarget = null;
|
||||
cmOrbitalFollow.HorizontalAxis.Center = transform.rotation.eulerAngles.y;
|
||||
cmOrbitalFollow.HorizontalAxis.TriggerRecentering();
|
||||
}
|
||||
|
||||
void UpdateLockOnUI(){
|
||||
|
||||
lockonTarget = null;
|
||||
lockonGameObject = null;
|
||||
}
|
||||
|
||||
void LateUpdate(){
|
||||
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>()
|
||||
);
|
||||
if (lockonGameObject) {
|
||||
// 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.Object.GetComponent<ILockOnTarget>().GetReticlePosition(),
|
||||
Camera.main
|
||||
);
|
||||
|
||||
// Set name
|
||||
elementLabelName.text = lockOnTarget.name;
|
||||
// Set name
|
||||
elementLabelName.text = lockonTarget.Object.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 position (add the width/height of the element)
|
||||
elementRoot.style.top = new StyleLength(screenPos.y - 25f); // Was 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);
|
||||
}
|
||||
// Set enabled
|
||||
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
|
||||
} else {
|
||||
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,9 +42,12 @@ public class PlayerControls : MonoBehaviour{
|
||||
|
||||
public void OnLockOn(){
|
||||
GetComponent<LockOnManager>().ChangeLockOnTarget();
|
||||
graph.SendEvent<string>("InputEvent", "LockOn", null);
|
||||
}
|
||||
|
||||
public void OnCancelLockOn(){
|
||||
GetComponent<LockOnManager>().RemoveLockOnTarget();
|
||||
graph.SendEvent<string>("InputEvent", "CancelLockOn", null);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user