changed: tweaks to make grapple feel better

This commit is contained in:
Chris
2025-08-17 14:37:33 -04:00
parent 6e4ab86d51
commit 93634a9586
8 changed files with 194 additions and 139 deletions

View File

@@ -1,7 +1,9 @@
using System;
using NUnit.Framework.Internal;
using UnityEngine;
using ParadoxNotion.Design;
using Sirenix.OdinInspector;
using UnityEditor.Rendering;
using UnityEngine.Serialization;
public enum PlayerFacingDirection{
@@ -71,11 +73,15 @@ namespace Reset.Units{
[ShowInInspector, ReadOnly] private float outputRotationSpeed;
private float directionChangeDot;
private bool moveCallDisabledNextFrame;
private CharacterController controller;
private PlayerControls controls;
private LockOnManager lockOnManager;
private Vector3 moveSmooth;
public float gravitySmooth;
[ShowInInspector, PropertyOrder(2)]
public UnitMovementData data = new();
@@ -94,11 +100,14 @@ namespace Reset.Units{
void Update(){
UpdateCurrentDirection();
UpdateCurrentSpeed();
UpdateCurrentGravity();
UpdateCurrentSpeed();
UpdateCurrentRotation();
DoMovement();
}
// Add directly to the direction
@@ -184,7 +193,7 @@ namespace Reset.Units{
}
// Commit move direction
outputMoveDirection = new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z);
outputMoveDirection = Vector3.SmoothDamp(outputMoveDirection, new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z),ref moveSmooth , .5f *Time.deltaTime);
}
// Update the speed, called every frame
@@ -195,8 +204,8 @@ namespace Reset.Units{
// Update the gravity, called every frame
private void UpdateCurrentGravity(){
// Accelerate gravity
data.gravityPower += data.gravityAcceleration * Time.deltaTime;
data.gravityPower = Mathf.Min(data.gravityPower, data.gravityMax);
// data.gravityPower += data.gravityAcceleration * Time.deltaTime;
data.gravityPower = Mathf.Clamp(data.gravityPower, Mathf.NegativeInfinity, data.gravityMax);
// Apply a constant gravity if the player is grounded
if (controller.isGrounded) {
@@ -207,7 +216,7 @@ namespace Reset.Units{
float gravityMoveDirection = data.jumpPower + (Physics.gravity.y * data.gravityPower);
// Commit gravity to move direction, ignoring XZ since those are done in UpdateMovementDirection
outputMoveDirection.y = gravityMoveDirection;
outputMoveDirection.y = Mathf.SmoothDamp(outputMoveDirection.y, gravityMoveDirection, ref gravitySmooth, .1f * Time.deltaTime);
}
// Update the rotation, called every frame
@@ -264,9 +273,24 @@ namespace Reset.Units{
public void DoMovement(){
DoMovement(outputMoveDirection, outputSpeed, data.gravityScale);
}
public void DisableNextMoveCall(){
moveCallDisabledNextFrame = true;
}
// Custom move from input
public void DoMovement(Vector3 moveDir, float speed, float gravity){
public void DoMovement(Vector3 moveDir, float speed, float gravityScale, bool relativeToCamera = true){
if (moveCallDisabledNextFrame) {
Vector3 velocity = transform.InverseTransformDirection(controller.velocity.normalized);
outputMoveDirection = new Vector3(velocity.x, outputMoveDirection.y, velocity.z);
moveCallDisabledNextFrame = false;
return;
}
Debug.Log($"moveDir: {moveDir}, agent velocity: {transform.InverseTransformDirection(controller.velocity.normalized)}");
// Seperate the different move directions. Additonal becomes it's own because it needs to be added independently of the other two
Vector3 moveXZDir = new Vector3(moveDir.x, 0f, moveDir.z);
Vector3 moveYDir = new Vector3(0f, moveDir.y, 0f);
@@ -274,13 +298,17 @@ namespace Reset.Units{
// Add their related speeds
moveXZDir *= speed * Time.deltaTime;
moveYDir *= gravity * Time.deltaTime;
moveYDir *= gravityScale * Time.deltaTime;
addDir *= additionalSpeed * Time.deltaTime;
// Construct the direction and move
Vector3 finalDir = moveXZDir + moveYDir;
controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * finalDir) + addDir);
if (relativeToCamera) {
controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * finalDir) + addDir);
} else {
controller.Move(finalDir + addDir);
}
}
void LateUpdate(){
@@ -308,6 +336,9 @@ namespace Reset.Units{
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
additionalMoveDirection = Vector3.Lerp(additionalMoveDirection, Vector3.zero, data.deaccelerationSmoothing * Time.deltaTime);
}
// Decay the gravity
additionalMoveDirection.y -= data.gravityPower;
}
private void UpdateGravityLate(){