changed: more robust environment observers, player graph changes

This commit is contained in:
Chris
2025-07-29 12:34:30 -04:00
parent 706e53c5f7
commit 92c6ac0016
4 changed files with 186 additions and 100 deletions

View File

@@ -9,6 +9,7 @@ namespace NodeCanvas.Tasks.Conditions {
public BBParameter<string> observerLabel;
public BBParameter<RaycastHit> outputHitTo;
//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(){

View File

@@ -9,9 +9,6 @@ using UnityEngine.Serialization;
[Serializable]
public class EnvironmentObserver{
// TODO: Clean this ugly shit up. Custom inspector with Odin plz.
public string label;
enum LabelDrawingLocation{
PlayerOffset,
HitLocation,
@@ -25,11 +22,13 @@ public class EnvironmentObserver{
BoxCast,
SphereCast
}
[PropertySpace(0, 5), LabelWidth(60)]
public string label;
[PropertySpace(0, 10), LabelWidth(60)]
public CastType castType;
// [BoxGroup("Settings")]
[ShowInInspector, SerializeField] public CastType castType;
[Button(ButtonSizes.Large), GUIColor("@GetObserverStatusColorStatic(active, hit)")]
[Button(ButtonSizes.Large), GUIColor("@GetObserverStatusColorStatic(active, hit)"), PropertyOrder(-1), PropertySpace(5, 5)]
public void Active(){
active = !active;
}
@@ -38,46 +37,44 @@ public class EnvironmentObserver{
public bool active;
// Parameters for Cast cast types
[BoxGroup("Settings")]
public float length;
[BoxGroup("Settings")]
public Vector3 direction;
[BoxGroup("Settings")]
public Vector3 offset;
[ShowIfGroup("Settings/Casts3D", VisibleIf = "@castType == CastType.Ray || castType == CastType.BoxCast || castType == CastType.SphereCast")]
[BoxGroup("Settings/Casts3D")]
public float width;
[FoldoutGroup("Settings")] public float length;
[FoldoutGroup("Settings")] public Vector3 direction;
[FoldoutGroup("Settings")] public Vector3 offset;
[PropertySpace(0, 5), FoldoutGroup("Settings")] public LayerMask ignoreLayers = ~0;
// Parameters for Overlap cast types
[BoxGroup("Settings/Overlaps")]
public Vector3 size;
[ShowIfGroup("Settings/CastsOnly", VisibleIf = "@castType == CastType.SphereCast || castType == CastType.SphereOverlap")]
[FoldoutGroup("Settings")] public float width;
[PropertyTooltip(
"Only use rotation when an observers direction alone can't (for whatever reason) fully be expressed without specifying a rotation.")]
[ShowIfGroup("Settings/Casts3D")]
[BoxGroup("Settings/Casts3D")]
// Parameters for Overlap cast types
[ShowIfGroup("Settings/3DOnly", VisibleIf = "@castType == CastType.BoxCast && castType != CastType.BoxOverlap")] [FoldoutGroup("Settings")]
public Vector3 size;
[ShowIfGroup("Settings/3DOnly")]
public Vector3 rotation;
[BoxGroup("Settings")]
public LayerMask ignoreLayers = ~0;
[HideInInspector]
public RaycastHit hit;
[SerializeReference]
public List<EnvironmentObserver> children;
[HideInInspector]
public Collider[] overlapHits;
public bool drawLabel;
public Vector3 labelLocationOffset;
public Vector3 labelRotationOffset;
[ShowInInspector, SerializeField]
LabelDrawingLocation labelLocation;
[FoldoutGroup("Text")]
[BoxGroup("Text/Label")] public bool drawLabel;
[ShowInInspector, SerializeField] [BoxGroup("Text/Label")] LabelDrawingLocation labelTextLocation;
[BoxGroup("Text/Label")] public float labelSize;
[BoxGroup("Text/Label")] public Vector3 labelLocationOffset;
[BoxGroup("Text/Label")] public Vector3 labelRotationOffset;
[BoxGroup("Text/Hit")] public bool drawHitName;
[ShowInInspector, SerializeField] [BoxGroup("Text/Hit")] LabelDrawingLocation hitTextLocation;
[BoxGroup("Text/Hit")] public float hitTextSize;
[BoxGroup("Text/Hit")] public Vector3 hitLocationOffset;
[BoxGroup("Text/Hit")] public Vector3 hitRotationOffset;
[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){
if (active) {
@@ -86,7 +83,7 @@ public class EnvironmentObserver{
// 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 relativeStartWithRotation = player.transform.position + player.transform.rotation * offset ;
switch (castType) {
case CastType.Ray:
@@ -105,11 +102,21 @@ public class EnvironmentObserver{
break;
case CastType.BoxCast:
// TODO: Make this not an if statement. Check that it works with NodeCanvas first
if (Physics.BoxCast(relativeStartWithRotation, Vector3.one * (width / 2f), (player.transform.rotation * Quaternion.Euler(rotation) * direction),
out hit, Quaternion.LookRotation(direction) * Quaternion.Euler(rotation), length, ignoreLayers)
);
if (Physics.BoxCast(relativeStartWithRotation, size / 2f,
player.transform.rotation * Quaternion.Euler(rotation) * direction,
out hit, player.transform.rotation * Quaternion.Euler(rotation), length,
ignoreLayers)
) {
};
break;
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,
out hit, length,
ignoreLayers)
) {
};
break;
}
@@ -122,29 +129,21 @@ public class EnvironmentObserver{
public void DrawObserverGizmo(GameObject player){
Vector3 relativeStart = player.transform.position + offset;
Vector3 relativeStartWithRotation = player.transform.position + player.transform.rotation * (offset) ;
Vector3 relativeStartWithRotation = player.transform.position + player.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;
// 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);
Vector3 firstBoxOffset = gizmosRotation * (Vector3.forward * size.z);
Color gizmoColor = Evaluate(player) ? Color.green : Color.red;
gizmoColor = active ? gizmoColor : Color.gray;
using (Draw.ingame.WithColor(gizmoColor)){
Vector3 labelStartPos = Vector3.zero;
switch (labelLocation) {
case LabelDrawingLocation.PlayerOffset:
labelStartPos = player.transform.position + labelLocationOffset;
break;
case LabelDrawingLocation.IntersectingLength:
labelStartPos = (relativeStart + (player.transform.rotation * direction * length) / 2f ) + labelLocationOffset;
break;
case LabelDrawingLocation.HitLocation:{
if (hit.transform != null) {
labelStartPos = hit.point + labelLocationOffset;
}
break;
}
}
switch (castType) {
case CastType.Ray:
Draw.ingame.Line(relativeStart, relativeStart + (player.transform.rotation * direction.normalized) * length);
@@ -152,35 +151,88 @@ public class EnvironmentObserver{
case CastType.BoxOverlap:
Draw.ingame.WireBox(relativeStartWithRotation, player.transform.rotation * Quaternion.Euler(rotation), size);
break;
case CastType.SphereOverlap:
case CastType.SphereCast:
Draw.ingame.SolidCircle(relativeStartWithRotation, relativeStartWithRotation - Camera.main.transform.position, width * 1, gizmoColor.Alpha(.5f));
Draw.ingame.WireCapsule(relativeStartWithRotation, relativeStartWithRotation + gizmosRotation * (Vector3.forward * (length - width / 2)), width);
break;
case CastType.BoxCast:
// 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 / 2f);
Vector3 offsetFromCenter = relativeStartWithRotation + player.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);
// Draw the gizmos for the boxcast
Draw.ingame.WireBox(relativeStartWithRotation, gizmosRotation, Vector3.one * width);
Draw.ingame.WireBox(offsetFromCenter, gizmosRotation, new float3(width, width, length + width));
Draw.ingame.WireBox(offsetFromCenter, gizmosRotation, new float3(size.x, size.y, length));
Draw.ingame.SolidBox(relativeStartWithRotation, gizmosRotation, size, gizmoColor.Alpha(.1f));
Draw.ingame.WireBox(relativeStartWithRotation, gizmosRotation, size);
break;
default:
throw new ArgumentOutOfRangeException();
}
Draw.ingame.SolidCircle(relativeStartWithRotation, relativeStartWithRotation - Camera.main.transform.position, .4f);
Draw.ingame.SolidCircle(hit.point, hit.point - Camera.main.transform.position, .4f);
Draw.ingame.Label3D(
labelStartPos,
player.transform.rotation * Quaternion.Euler(labelRotationOffset),
hit.collider == null ? "" : hit.collider.name,
.5f,
LabelAlignment.Center,
Color.black
);
// Set up variables for label (not hit name)
Vector3 labelStartPos = Vector3.zero;
switch (labelTextLocation) {
case LabelDrawingLocation.PlayerOffset:
labelStartPos = player.transform.position;
break;
case LabelDrawingLocation.IntersectingLength:
labelStartPos = offsetFromCenter;
break;
case LabelDrawingLocation.HitLocation:{
if (hit.transform != null) {
labelStartPos = hit.point;
}
break;
}
}
// Draw label
if (drawLabel) {
Draw.ingame.Label3D(
labelStartPos + labelLocationOffset,
gizmosRotation * Quaternion.Euler(labelRotationOffset),
label,
labelSize,
LabelAlignment.MiddleLeft,
gizmoColor
);
}
// Set up variables for hit name
// Since the label is already drawn just use the previous startPos
switch (labelTextLocation) {
case LabelDrawingLocation.PlayerOffset:
labelStartPos = player.transform.position;
break;
case LabelDrawingLocation.IntersectingLength:
labelStartPos = offsetFromCenter;
break;
case LabelDrawingLocation.HitLocation:{
if (hit.transform != null) {
labelStartPos = hit.point;
}
break;
}
}
// Draw hitname
if (drawLabel) {
Draw.ingame.Label3D(
labelStartPos + labelLocationOffset,
gizmosRotation * Quaternion.Euler(labelRotationOffset),
label,
hitTextSize,
LabelAlignment.MiddleLeft,
gizmoColor
);
}
Sirenix.Utilities.Editor.GUIHelper.RequestRepaint();
}
}
static Color GetObserverStatusColorStatic(bool active, RaycastHit hit){