change: more grapple work

This commit is contained in:
Chris
2025-08-24 21:03:18 -04:00
parent 99eacd87e5
commit 45a1feb388
2 changed files with 194 additions and 263 deletions

View File

@@ -34,7 +34,7 @@ namespace NodeCanvas.Tasks.Actions {
private float startTime;
private Vector3 velocityOnStart;
private Vector3 directionOnStart;
private Vector3 originalDirection;
@@ -49,6 +49,7 @@ namespace NodeCanvas.Tasks.Actions {
private Vector3 gizmoPointDirection;
private Vector3 gizmoFinalDirection;
private Vector3 gizmosSmoothedInput;
private float gizmoVertValue;
private Transform camera;
@@ -70,13 +71,19 @@ namespace NodeCanvas.Tasks.Actions {
protected override void OnExecute(){
camera = Camera.main.transform;
MonoManager.current.onLateUpdate += DrawGrappleGizmo;
// Set the initial direction
directionOnStart = agent.transform.position.DirectionTo(grapplePoint.value);
// Get the current move direction
velocityOnStart = agent.outputMoveDirection;
// Use the current move direciton to initialize the direction
finalDirection = velocityOnStart;
// Lerp the initial direction more towards the point of the grapple and less towards current momentum if not moving fast
finalDirection = Vector3.Lerp(velocityOnStart, directionOnStart, velocityOnStart.magnitude / 10f);
startTime = Time.time;
currentSpeed = pullSpeedCurve.value[0].value * pullSpeedRange.value.y;
@@ -85,39 +92,55 @@ namespace NodeCanvas.Tasks.Actions {
//Called once per frame while the action is active.
protected override void OnUpdate(){
// Get direction to the point
Vector3 directionToPoint = agent.transform.position.DirectionTo(grapplePoint.value);
// Calculate input
Vector2 rawInput = agent.GetComponent<PlayerControls>().rawMoveInput;
Vector3 input = new Vector3(rawInput.x, rawInput.y, 0f);
Vector3 input = new(rawInput.x, rawInput.y, 0f);
smoothedInput = Vector3.SmoothDamp(smoothedInput, input, ref smoothedInputRefVelocity, 1f);
smoothedInput = Vector3.SmoothDamp(smoothedInput, input, ref smoothedInputRefVelocity, 2);
// Change input handling based on position
if (directionToPoint.y < 0) {
} else {
}
// Create the distance variables
float currentDist = Vector3.Distance(agent.transform.position, grapplePoint.value);
// Calculate the direction on XZ
Vector3 directionToPoint = agent.transform.position.DirectionTo(grapplePoint.value);
Vector3 directionToSwing = Quaternion.LookRotation(input) * directionToPoint;
// Calculate the swing direction.
// Vector3 swingDirection = Quaternion.LookRotation(smoothedInput) * directionToPoint * smoothedInput.magnitude; // Old
Vector3 sidewaysSwingAngle = Quaternion.AngleAxis(90f, Vector3.up) * directionToPoint;
Vector3 upwardsSwingAngle = Quaternion.AngleAxis(-90f, Vector3.right) * directionToPoint;
// Vector3 downwardsSwingAngle = Quaternion.AngleAxis(90f, Vector3.right) * directionToPoint; // why not just use upwards but negative based on input on Y
finalDirection = Vector3.Slerp(directionToPoint, directionToSwing, (elapsedTime * .6f));
// Flatten it
// finalDirection.Flatten(null, 0f); // don't wanna flatten it to use the natural y as gravity
// Create the composite swing direction
Vector3 compositeSwingDirection;
// if (directionToPoint.y > 0) {
Vector3 swingAngleAbovePoint = Vector3.Slerp(upwardsSwingAngle, -upwardsSwingAngle, Mathf.Abs((smoothedInput.y + 1f) / 2));
Vector3 axisFromInput = Vector3.zero;
if (smoothedInput.x >= 0) {
axisFromInput = Vector3.Slerp(swingAngleAbovePoint, sidewaysSwingAngle, smoothedInput.x);
}
if (smoothedInput.x < 0) {
axisFromInput = Vector3.Slerp(swingAngleAbovePoint, -sidewaysSwingAngle, Mathf.Abs(smoothedInput.x));
}
compositeSwingDirection = Vector3.Slerp(swingAngleAbovePoint, axisFromInput, smoothedInput.x);
// } else {
// compositeSwingDirection = Vector3.Slerp(upwardsSwingAngle, sidewaysSwingAngle, smoothedInput.magnitude);
// }
// Some math for getting the Y
yChangeMultipler = Mathf.Lerp(yChangeMultipler, 0f, elapsedTime * .5f); // Starts at 1 so that the player has more ability to change height on start of swing, then smooths to zero
// Add Y, this is a test
if (Vector3.Dot(finalDirection, Vector3.down) < 0) { // This should make you go up
float yTarget = 10f;
finalDirection = Vector3.Slerp(finalDirection, finalDirection.Flatten(null, yTarget), 1f * Time.deltaTime);
} else {
float yTarget = -10f;
finalDirection = Vector3.Slerp(finalDirection, finalDirection.Flatten(null, yTarget), 1f * Time.deltaTime);
}
Debug.Log(Vector3.Dot(directionToSwing, Vector3.down));
Debug.Log(Vector3.Dot(directionToPoint, Vector3.down));
// Speed
float evaluatedSpeed = pullSpeedCurve.value.Evaluate(Mathf.Clamp((Time.time - startTime) / 6f, 0f, Mathf.Infinity));
@@ -144,20 +167,25 @@ namespace NodeCanvas.Tasks.Actions {
currentSpeed = Mathf.Lerp(currentSpeed, speedAgainstCurve, 10f * Time.deltaTime);
// Gizmos
gizmoVertValue = finalDirection.y;
gizmosSmoothedInput = smoothedInput;
gizmoPointDirection = directionToPoint;
gizmoSwingDirection = directionToSwing;
gizmoSwingDirection = compositeSwingDirection;
gizmoFinalDirection = finalDirection;
agent.SetNewDirection(finalDirection.normalized.Flatten(null, 0));
//Test
finalDirection = gizmoSwingDirection;
agent.SetNewDirection(finalDirection.Flatten(null, 0));
agent.SetNewGravity(finalDirection.y);
agent.SmoothToSpeed(5f, 1f * Time.deltaTime, out referenceSpeed);
agent.SmoothToSpeed(0f, 1f * Time.deltaTime, out referenceSpeed);
// agent.SmoothToDirection(finalDirection.Flatten(null, 0).normalized * evaluatedSpeed, 1f * Time.deltaTime, out referenceDirection);
// agent.SmoothToGravitation(finalDirection.y, 1f, out referenceGravity);
if (Vector3.Dot(velocityOnStart, directionToPoint.normalized) < breakAtDotProduct) {
EndAction(true);
if (Vector3.Dot(directionOnStart, directionToPoint.normalized) < breakAtDotProduct) {
// EndAction(true);
} else if (currentDist < breakAtDistance) {
EndAction(true);
}
@@ -177,9 +205,9 @@ namespace NodeCanvas.Tasks.Actions {
Draw.Line(agent.transform.position + Vector3.up, agent.transform.position + Vector3.up + gizmoFinalDirection.normalized * 2f);
Draw.ArrowheadArc(agent.transform.position + Vector3.up, gizmoFinalDirection.normalized, 2f, 15f);
Color inactiveBlue = new(0, 0, 1, .1f);
Color swingColor = Color.Lerp(Color.blue, inactiveBlue, (elapsedTime * .6f));
Color dirColor = Color.Lerp(inactiveBlue, Color.blue, (elapsedTime * .6f));
// Colors for faded arrows
Color swingColor = Color.Lerp(Color.blue, Color.blue.Alpha(.4f), (elapsedTime * .6f));
Color dirColor = Color.Lerp(Color.blue.Alpha(.4f), Color.blue, (elapsedTime * .6f));
// Swing Direction
using (Draw.WithColor(swingColor)) {
@@ -211,6 +239,32 @@ namespace NodeCanvas.Tasks.Actions {
Draw.Line(inputGizmoOffset + camera.rotation * Vector3.left, inputGizmoOffset + camera.rotation * Vector3.right);
Draw.SolidCircle(inputGizmoPosition, inputGizmoPosition.DirectionTo(camera.position), Mathf.Lerp(.2f, .6f, (gizmosSmoothedInput.y - 1 ) / 2f + 1f));
}
// Up and down
using (Draw.WithColor(Color.yellow)) {
Vector3 vertGizmoPosition = agent.transform.position + camera.rotation * Vector3.left;
float vertGizmoWidth = .25f;
float vertGizmoHeight = 1.75f;
Vector3 vertGizmoStart = vertGizmoPosition + Vector3.up * vertGizmoHeight;
Vector3 vertGizmoEnd = vertGizmoPosition + Vector3.up * .35f;
Vector3 circlePos = Vector3.Lerp(vertGizmoEnd, vertGizmoStart, gizmoVertValue);
Draw.Line(vertGizmoStart, vertGizmoEnd);
Draw.SolidCircle(circlePos, circlePos.DirectionTo(camera.position), .4f);
Draw.Label2D(vertGizmoStart + camera.rotation * Vector3.left * 1.5f, gizmoVertValue.ToString());
Vector3 vertArrowUpPosition = vertGizmoStart + camera.rotation * Vector3.left * 1f + Vector3.up * .2f;
Vector3 vertArrowDownPosition = vertGizmoStart + camera.rotation * Vector3.left * 1f + Vector3.down * .2f;
if (gizmoVertValue > 0) {
Draw.SolidTriangle(vertArrowUpPosition + camera.rotation * Vector3.left/4, vertArrowUpPosition + Vector3.up/2, vertArrowUpPosition + camera.rotation * Vector3.right/4);
} else {
Draw.SolidTriangle(vertArrowDownPosition + camera.rotation * Vector3.left/4, vertArrowDownPosition + Vector3.down/2, vertArrowDownPosition + camera.rotation * Vector3.right/4);
}
}
}
//Called when the task is disabled.