fix: more movement and observer tweaks and fixes

This commit is contained in:
Chris
2025-07-30 13:22:36 -04:00
parent ae1908013d
commit 598fa9f6fc
8 changed files with 188 additions and 76 deletions

View File

@@ -1,47 +0,0 @@
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
namespace NodeCanvas.Tasks.Conditions {
[Category("Core/Input")]
[Description("Returns a float from two Vector3s")]
public class GetMovementInputDotProduct : ConditionTask<PlayerControls>{
[SliderField(0f,1f)]
public BBParameter<Vector3> desiredVector3;
public BBParameter<float> tolerance;
public BBParameter<bool> negate;
//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(){
return null;
}
//This is called once each time the task is enabled.
//Call EndAction() to mark the action as finished, either in success or failure.
//EndAction can be called from anywhere.
protected override bool OnCheck(){
Vector3 rawInputVector3 = new(agent.rawMoveInput.x, 0f, agent.rawMoveInput.y);
float dotProduct = Vector3.Dot(desiredVector3.value, rawInputVector3);
//Debug.Log(dotProduct);
if (dotProduct < tolerance.value) {
return true;
}
return false;
}
//Called when the task is disabled.
protected override void OnEnable() {
}
//Called when the task is paused.
protected override void OnDisable() {
}
}
}

View File

@@ -0,0 +1,63 @@
using System.Drawing;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Reset {
[Category("Reset")]
[Description("Creates an environment observer unattached from the player, such as for checking from the Camera or another arbitray location.")]
public class CheckGenericObserver : ConditionTask<Transform>{
[Space(5)]
public BBParameter<EnvironmentObserver.CastType> castType;
public BBParameter<float> length;
public BBParameter<Vector3> direction;
public BBParameter<Vector3> offset;
public BBParameter<LayerMask> ignoreLayers;
[ShowIf("castType", ((int)EnvironmentObserver.CastType.SphereCast|(int)EnvironmentObserver.CastType.SphereOverlap))]
public BBParameter<float> width;
public BBParameter<Vector3> size;
public BBParameter<Vector3> rotation;
private EnvironmentObserver observer;
//Return null if init was successfull. Return an error string otherwise
protected override string OnInit(){
return null;
}
//Called whenever the condition gets enabled.
protected override void OnEnable() {
observer = new EnvironmentObserver(){
castType = EnvironmentObserver.CastType.SphereCast,
length = length.value,
direction = direction.value,
offset = offset.value,
width = width.value,
size = size.value,
rotation = rotation.value
};
}
//Called whenever the condition gets disabled.
protected override void OnDisable() {
}
//Called once per frame while the condition is active.
//Return whether the condition is success or failure.
protected override bool OnCheck() {
return observer.Evaluate(agent.gameObject);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: fb4b5bf056649ca48b1b14dbe499de46

View File

@@ -0,0 +1,85 @@
using System;
using NodeCanvas.Framework;
using NUnit.Framework.Constraints;
using ParadoxNotion.Design;
using ParadoxNotion.Serialization.FullSerializer;
using Sirenix.OdinInspector;
using UnityEngine;
namespace NodeCanvas.Tasks.Conditions {
[Category("Reset/Input")]
[Description("Returns a float from two Vector3s")]
public class GetMovementInputDotProduct : ConditionTask<PlayerControls>{
enum CheckDotProductAgainst{
ForwardDirection,
CameraDirection,
InputVector3
}
[ExposeField, fsSerializeAs] CheckDotProductAgainst checkAgainst;
[ParadoxNotion.Design.ShowIf("checkAgainst", 2)] public BBParameter<Vector3> checkAgainstValue;
[SliderField(-1f, 1f)] public BBParameter<float> desiredValue;
public BBParameter<float> tolerance;
public BBParameter<bool> considerCameraRotation;
public BBParameter<bool> negate;
//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(){
return null;
}
//This is called once each time the task is enabled.
//Call EndAction() to mark the action as finished, either in success or failure.
//EndAction can be called from anywhere.
protected override bool OnCheck(){
// Switch what the dot product is checked against
Vector3 valueToCheck;
switch (checkAgainst) {
case CheckDotProductAgainst.ForwardDirection:
Debug.Log(agent.transform.forward);
valueToCheck = agent.transform.forward;
break;
case CheckDotProductAgainst.CameraDirection:
valueToCheck = Camera.main.transform.forward;
break;
case CheckDotProductAgainst.InputVector3:
valueToCheck = checkAgainstValue.value.normalized;
break;
default:
throw new ArgumentOutOfRangeException();
}
// Get the input as a Vector3
Vector3 rawInputVector3 = new Vector3(agent.rawMoveInput.x, 0f, agent.rawMoveInput.y);
if (considerCameraRotation.value) {
rawInputVector3 = Camera.main.transform.rotation * rawInputVector3;
}
// Calculate dor product
float dotProduct = Vector3.Dot(valueToCheck, rawInputVector3);
Debug.Log(dotProduct);
// Compare against the desired tolerance and output result
if (tolerance.value > Mathf.Abs(dotProduct - tolerance.value)) {
return true;
}
return false;
}
//Called when the task is disabled.
protected override void OnEnable() {
}
//Called when the task is paused.
protected override void OnDisable() {
}
}
}

View File

@@ -90,13 +90,16 @@ namespace NodeCanvas.Tasks.Actions {
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
if (agent.isGrounded){
if (controls.rawMoveInput.magnitude == 0) { break; }
targetRotation = Quaternion.LookRotation(currentMoveDir) *
Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0f, null, 0f));
} else {
if (airMoveDirection.value.magnitude == 0) { break; }
targetRotation = Quaternion.LookRotation(airMoveDirection.value);
}
break;

View File

@@ -15,6 +15,12 @@ public class EnvironmentObserver{
IntersectingLength,
}
enum ObserverGizmoDrawingCondition{
Always,
OnlyActive,
Never
}
public enum CastType{
Ray,
BoxOverlap,
@@ -41,7 +47,6 @@ public class EnvironmentObserver{
[FoldoutGroup("Settings")] public Vector3 direction;
[FoldoutGroup("Settings")] public Vector3 offset;
[PropertySpace(0, 5), FoldoutGroup("Settings")] public LayerMask ignoreLayers = ~0;
[ShowIfGroup("Settings/CastsOnly", VisibleIf = "@castType == CastType.SphereCast || castType == CastType.SphereOverlap")]
[FoldoutGroup("Settings")] public float width;
@@ -71,27 +76,28 @@ public class EnvironmentObserver{
[BoxGroup("Text/Hit")] public float hitTextSize;
[BoxGroup("Text/Hit")] public Vector3 hitLocationOffset;
[BoxGroup("Text/Hit")] public Vector3 hitRotationOffset;
[FoldoutGroup("Text"), SerializeField, ShowInInspector] ObserverGizmoDrawingCondition gizmoDrawingCondition;
[SerializeReference, PropertySpace(5, 5)]
public List<EnvironmentObserver> children;
// NOTE: I had a ref for a RaycastHit here that would correspond to hit but idk if it's needed.
public bool Evaluate(GameObject player){
public bool Evaluate(GameObject source){
if (active) {
// Remove player's layer from LayerMask.
ignoreLayers -= player.layer;
ignoreLayers -= source.layer;
// Set some of the variables used later during casting
Vector3 relativeStart = player.transform.position + offset;
Vector3 relativeStartWithRotation = player.transform.position + player.transform.rotation * offset ;
Vector3 relativeStart = source.transform.position + offset;
Vector3 relativeStartWithRotation = source.transform.position + source.transform.rotation * offset ;
switch (castType) {
case CastType.Ray:
Physics.Raycast(relativeStart, player.transform.rotation * direction, out hit, length, ignoreLayers);
Physics.Raycast(relativeStart, source.transform.rotation * direction, out hit, length, ignoreLayers);
break;
case CastType.BoxOverlap:
overlapHits = Physics.OverlapBox(relativeStartWithRotation, size / 2f,
player.transform.rotation * Quaternion.Euler(rotation), ignoreLayers);
source.transform.rotation * Quaternion.Euler(rotation), ignoreLayers);
if (overlapHits.Length > 0) {
return true;
@@ -103,8 +109,8 @@ public class EnvironmentObserver{
case CastType.BoxCast:
// TODO: Make this not an if statement. Check that it works with NodeCanvas first
if (Physics.BoxCast(relativeStartWithRotation, size / 2f,
player.transform.rotation * Quaternion.Euler(rotation) * direction,
out hit, player.transform.rotation * Quaternion.Euler(rotation), length,
source.transform.rotation * Quaternion.Euler(rotation) * direction,
out hit, source.transform.rotation * Quaternion.Euler(rotation), length,
ignoreLayers)
) {
};
@@ -112,7 +118,7 @@ public class EnvironmentObserver{
case CastType.SphereCast:
// TODO: Make this not an if statement. Check that it works with NodeCanvas first
if (Physics.SphereCast(relativeStartWithRotation, width / 2f,
player.transform.rotation * Quaternion.Euler(rotation) * direction,
source.transform.rotation * Quaternion.Euler(rotation) * direction,
out hit, length,
ignoreLayers)
) {
@@ -127,29 +133,34 @@ public class EnvironmentObserver{
return false;
}
public void DrawObserverGizmo(GameObject player){
Vector3 relativeStart = player.transform.position + offset;
Vector3 relativeStartWithRotation = player.transform.position + player.transform.rotation * (offset);
public void DrawObserverGizmo(GameObject source){
if (gizmoDrawingCondition == ObserverGizmoDrawingCondition.Never ||
(gizmoDrawingCondition == ObserverGizmoDrawingCondition.OnlyActive ! & active)) {
return;
}
Vector3 relativeStart = source.transform.position + offset;
Vector3 relativeStartWithRotation = source.transform.position + source.transform.rotation * (offset);
// Setup the variables for boxcast, spherecast, etc
// Create an offset start point for the center of the wirebox, since gizmos are drawn with their pivot in the center.
Vector3 offsetWithRotationAndLength = (Quaternion.LookRotation(direction) * Quaternion.Euler(rotation)) * (Vector3.forward * (length / 2));
Vector3 offsetFromCenter = relativeStartWithRotation + player.transform.rotation * offsetWithRotationAndLength;
Vector3 offsetFromCenter = relativeStartWithRotation + source.transform.rotation * offsetWithRotationAndLength;
// Also create a rotation for use with the gizmos. Mainly just to shorten the lines
Quaternion gizmosRotation = player.transform.rotation * Quaternion.LookRotation(direction) * Quaternion.Euler(rotation);
Quaternion gizmosRotation = source.transform.rotation * Quaternion.LookRotation(direction) * Quaternion.Euler(rotation);
Vector3 firstBoxOffset = gizmosRotation * (Vector3.forward * size.z);
Color gizmoColor = Evaluate(player) ? Color.green : Color.red;
Color gizmoColor = Evaluate(source) ? Color.green : Color.red;
gizmoColor = active ? gizmoColor : Color.gray;
using (Draw.ingame.WithColor(gizmoColor)){
switch (castType) {
case CastType.Ray:
Draw.ingame.Line(relativeStart, relativeStart + (player.transform.rotation * direction.normalized) * length);
Draw.ingame.Line(relativeStart, relativeStart + (source.transform.rotation * direction.normalized) * length);
break;
case CastType.BoxOverlap:
Draw.ingame.WireBox(relativeStartWithRotation, player.transform.rotation * Quaternion.Euler(rotation), size);
Draw.ingame.WireBox(relativeStartWithRotation, source.transform.rotation * Quaternion.Euler(rotation), size);
break;
case CastType.SphereCast:
Draw.ingame.SolidCircle(relativeStartWithRotation, relativeStartWithRotation - Camera.main.transform.position, width * 1, gizmoColor.Alpha(.5f));
@@ -173,7 +184,7 @@ public class EnvironmentObserver{
Vector3 labelStartPos = Vector3.zero;
switch (labelTextLocation) {
case LabelDrawingLocation.PlayerOffset:
labelStartPos = player.transform.position;
labelStartPos = source.transform.position;
break;
case LabelDrawingLocation.IntersectingLength:
labelStartPos = offsetFromCenter;
@@ -203,7 +214,7 @@ public class EnvironmentObserver{
// Since the label is already drawn just use the previous startPos
switch (labelTextLocation) {
case LabelDrawingLocation.PlayerOffset:
labelStartPos = player.transform.position;
labelStartPos = source.transform.position;
break;
case LabelDrawingLocation.IntersectingLength:
labelStartPos = offsetFromCenter;
@@ -228,11 +239,7 @@ public class EnvironmentObserver{
gizmoColor
);
}
Sirenix.Utilities.Editor.GUIHelper.RequestRepaint();
}
}
static Color GetObserverStatusColorStatic(bool active, RaycastHit hit){
@@ -320,7 +327,6 @@ public class PlayerEnvironmentManager : MonoBehaviour{
}
void Update(){
Debug.Log(EvaluateFromString("gamedev"));
}
void LateUpdate(){