maint: cleaned up some files and tweaked some movement
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Drawing;
|
||||
using Reset.Movement;
|
||||
using ParadoxNotion.Design;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
public enum PlayerFacingDirection{
|
||||
TowardsTarget = 0,
|
||||
@@ -25,10 +24,13 @@ namespace Reset.Units{
|
||||
// Movement Direction
|
||||
public float accelerationSmoothing = 5f;
|
||||
public float deaccelerationSmoothing = 5f;
|
||||
public AnimationCurve deaccelerationCurve;
|
||||
// public AnimationCurve deaccelerationCurve; // Currently unused, may return
|
||||
|
||||
[SliderField(0,1)]
|
||||
public float airDirectionDecay;
|
||||
|
||||
// Move Speed
|
||||
public float moveSpeedTarget = 15f;
|
||||
public float moveSpeed = 15f;
|
||||
public float moveSpeedSoothing = 10f;
|
||||
|
||||
// Jumping
|
||||
@@ -44,11 +46,12 @@ namespace Reset.Units{
|
||||
// Rotation
|
||||
[ShowInInspector, SerializeReference]
|
||||
public Enum rotateFacing;
|
||||
public float rotationSpeedTarget = 5f;
|
||||
[FormerlySerializedAs("rotationSpeedTarget")] public float rotationSpeed = 5f;
|
||||
public float rotationSmoothing = 1f;
|
||||
public float rotationInputBlending = .3f;
|
||||
|
||||
public object Clone(){
|
||||
return this.MemberwiseClone();
|
||||
return MemberwiseClone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,32 +62,28 @@ namespace Reset.Units{
|
||||
// }
|
||||
|
||||
// Debug viewing
|
||||
// Smoothed Values
|
||||
[ShowInInspector, ReadOnly] private float outputSpeed;
|
||||
[ShowInInspector, ReadOnly] private float additionalSpeed;
|
||||
[ShowInInspector, ReadOnly] private Vector3 outputMoveDirection;
|
||||
[ShowInInspector, ReadOnly] public Vector3 outputMoveDirection;
|
||||
[ShowInInspector, ReadOnly] private Vector3 additionalMoveDirection;
|
||||
[ShowInInspector, ReadOnly] private Quaternion outputRotation;
|
||||
[ShowInInspector, ReadOnly] private Quaternion specifiedRotation;
|
||||
[ShowInInspector, ReadOnly] private float outputRotationSpeed;
|
||||
|
||||
|
||||
private CharacterController controller;
|
||||
public PlayerControls controls;
|
||||
private PlayerControls controls;
|
||||
private LockOnManager lockOnManager;
|
||||
|
||||
[ShowInInspector, PropertyOrder(2)]
|
||||
public UnitMovementData data = new UnitMovementData();
|
||||
public UnitMovementData data = new();
|
||||
|
||||
[HideInInspector]
|
||||
public UnitMovementData defaultData;
|
||||
|
||||
[Button, PropertyOrder(1)]
|
||||
void ResetMovementData(){
|
||||
data = new UnitMovementData();
|
||||
}
|
||||
|
||||
|
||||
void Awake(){
|
||||
controller = GetComponent<CharacterController>();
|
||||
controls = GetComponent<PlayerControls>();
|
||||
lockOnManager = GetComponent<LockOnManager>();
|
||||
}
|
||||
|
||||
private void Start(){
|
||||
@@ -100,15 +99,40 @@ namespace Reset.Units{
|
||||
DoMovement();
|
||||
}
|
||||
|
||||
// Add directly to the direction
|
||||
public void AddToCurrentDirection(Vector3 inputDirection, float power){
|
||||
additionalMoveDirection += inputDirection.normalized;
|
||||
additionalSpeed = power;
|
||||
}
|
||||
|
||||
// Hold a new rotation to be moved to during PlayerFacingDirection.SpecifiedRotation
|
||||
public void SetSpecifiedRotation(Quaternion inputRotation){
|
||||
specifiedRotation = inputRotation;
|
||||
}
|
||||
|
||||
// Blend between the current direction and an input direction, based on the current controller input
|
||||
public void OverwriteDirectionFromInput(Vector2 value, float priority, float speed = Mathf.Infinity){
|
||||
// Create a new direction that is the current controller input * the amount of power they should have
|
||||
Vector3 dirToAdd = new Vector3(controls.rawMoveInput.x * value.x, 0f, controls.rawMoveInput.y * value.y);
|
||||
|
||||
// Multiply it by the current magnitude (why? i forgor)
|
||||
dirToAdd *= controls.rawMoveInput.magnitude;
|
||||
|
||||
// Blend the existing direction into it
|
||||
dirToAdd = Vector3.Lerp(outputMoveDirection, dirToAdd, priority * controls.rawMoveInput.magnitude);
|
||||
|
||||
// Set the new direction
|
||||
outputMoveDirection = new Vector3(dirToAdd.x, outputMoveDirection.y, dirToAdd.z);
|
||||
|
||||
// Everthing under here is for the speed now. If it's not set do...nothing. Otherwise set it to the max value between move speed and the new speed
|
||||
if (float.IsPositiveInfinity(speed)) {
|
||||
return;
|
||||
}
|
||||
|
||||
outputSpeed = Mathf.Lerp(outputSpeed, Mathf.Max(data.moveSpeed, speed), priority);
|
||||
}
|
||||
|
||||
// Update the direction, called every frame
|
||||
private void UpdateCurrentDirection(){
|
||||
// Get input value
|
||||
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
|
||||
@@ -116,7 +140,8 @@ namespace Reset.Units{
|
||||
// Construct move direction
|
||||
Vector3 targetDirection = inputMovement;
|
||||
|
||||
if (inputMovement.magnitude < .1f) {
|
||||
// Deadzone
|
||||
if (inputMovement.magnitude < .05f) {
|
||||
targetDirection = Vector3.zero;
|
||||
}
|
||||
|
||||
@@ -124,24 +149,28 @@ namespace Reset.Units{
|
||||
Vector3 targetNoY = new Vector3(targetDirection.x, 0f, targetDirection.z);
|
||||
Vector3 currentNoY = new Vector3(outputMoveDirection.x, 0f, outputMoveDirection.z);
|
||||
|
||||
// Smooth movement
|
||||
// Smooth movement. Use deaccel smoothing if the input magnitude is lower, and accel smoothing if it's higher
|
||||
// Also checks when grounded to only use Slerp on the ground
|
||||
if (targetNoY.magnitude > currentNoY.magnitude) {
|
||||
currentNoY = Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime);
|
||||
|
||||
currentNoY = controller.isGrounded ?
|
||||
Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime) :
|
||||
Vector3.Lerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime);
|
||||
} else {
|
||||
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
||||
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime);
|
||||
currentNoY = controller.isGrounded ?
|
||||
Vector3.Slerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime) :
|
||||
Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * data.airDirectionDecay * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Commit move direction
|
||||
outputMoveDirection = new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z);
|
||||
}
|
||||
|
||||
// Update the speed, called every frame
|
||||
private void UpdateCurrentSpeed(){
|
||||
outputSpeed = Mathf.Lerp(outputSpeed, data.moveSpeedTarget, data.moveSpeedSoothing * Time.deltaTime);
|
||||
outputSpeed = Mathf.Lerp(outputSpeed, data.moveSpeed, data.moveSpeedSoothing * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Update the gravity, called every frame
|
||||
private void UpdateCurrentGravity(){
|
||||
// Accelerate gravity
|
||||
data.gravityPower += data.gravityAcceleration * Time.deltaTime;
|
||||
@@ -159,58 +188,74 @@ namespace Reset.Units{
|
||||
outputMoveDirection.y = gravityMoveDirection;
|
||||
}
|
||||
|
||||
// Update the rotation, called every frame
|
||||
private void UpdateCurrentRotation(){
|
||||
// Get input value
|
||||
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
|
||||
|
||||
// Switch the desired rotation based on current movement setting
|
||||
switch (data.rotateFacing) {
|
||||
// TODO: Recomment
|
||||
// Just look at target
|
||||
case PlayerFacingDirection.TowardsTarget:
|
||||
Debug.LogError("Not implemented...");
|
||||
// Look directly at the target
|
||||
outputRotation = Quaternion.LookRotation(transform.position.DirectionTo(lockOnManager.mainTarget.gameObject.transform.position));
|
||||
break;
|
||||
case PlayerFacingDirection.Momentum:
|
||||
if (inputMovement.magnitude > .03f){
|
||||
// Look towards the current direction the agent is moving
|
||||
if (inputMovement.magnitude > .05f){
|
||||
outputRotation = Camera.main.transform.rotation * Quaternion.LookRotation(outputMoveDirection);
|
||||
}
|
||||
break;
|
||||
case PlayerFacingDirection.MatchForward:
|
||||
if (controls.rawMoveInput.magnitude < 0.01f) { break; }
|
||||
// Look towards the input direction....why??? I guess cause move direction is PlayerFacingDirection.Momentum.
|
||||
if (controls.rawMoveInput.magnitude < 0.05f) { break; }
|
||||
|
||||
outputRotation = Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement);
|
||||
|
||||
break;
|
||||
case PlayerFacingDirection.MatchCamera:
|
||||
|
||||
// Look the same direction as the camera
|
||||
outputRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0));
|
||||
break;
|
||||
case PlayerFacingDirection.Static:
|
||||
// Don't change
|
||||
outputRotation = transform.rotation;
|
||||
break;
|
||||
case PlayerFacingDirection.SpecifiedDirection:
|
||||
// Look at an inputed rotation
|
||||
outputRotation = specifiedRotation;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate rotation speed
|
||||
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeedTarget, data.rotationSmoothing * Time.deltaTime);
|
||||
// Add the current input into the created rotation
|
||||
if (inputMovement.magnitude > .05){
|
||||
outputRotation = Quaternion.Lerp(outputRotation, Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement), data.rotationInputBlending);
|
||||
}
|
||||
|
||||
// Calculate rotation speed, as in how fast the fella rotates. This value has it's own smoothing to allow for gradual increasing/decreasing of the speed till it reaches the target
|
||||
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeed, data.rotationSmoothing * Time.deltaTime);
|
||||
|
||||
// Set final rotation
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0);
|
||||
}
|
||||
|
||||
// Move with default settings
|
||||
public void DoMovement(){
|
||||
DoMovement(outputMoveDirection, outputSpeed, data.gravityScale);
|
||||
}
|
||||
|
||||
// Custom move from input
|
||||
public void DoMovement(Vector3 moveDir, float speed, float gravity){
|
||||
// Commit the move, with respect to the camera's rotation
|
||||
// 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);
|
||||
Vector3 addDir = additionalMoveDirection;
|
||||
|
||||
// Add their related speeds
|
||||
moveXZDir *= speed * Time.deltaTime;
|
||||
moveYDir *= gravity * 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);
|
||||
@@ -233,12 +278,12 @@ namespace Reset.Units{
|
||||
// Remove Y from variables
|
||||
Vector3 currentNoY = new Vector3(additionalMoveDirection.x, 0f, additionalMoveDirection.z);
|
||||
|
||||
// Decay the direction
|
||||
if (inputMovement.magnitude < currentNoY.magnitude) {
|
||||
additionalMoveDirection = Vector3.Slerp(additionalMoveDirection, Vector3.zero,data.accelerationSmoothing * Time.deltaTime);
|
||||
|
||||
} else {
|
||||
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
||||
|
||||
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
||||
additionalMoveDirection = Vector3.Lerp(additionalMoveDirection, Vector3.zero, data.deaccelerationSmoothing * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
@@ -248,10 +293,6 @@ namespace Reset.Units{
|
||||
data.jumpPower -= data.jumpPowerDecay * Time.deltaTime;
|
||||
data.jumpPower = Mathf.Max(0f, data.jumpPower);
|
||||
}
|
||||
|
||||
public void AssignNewData(UnitMovementData movementData){
|
||||
data = (UnitMovementData)movementData.Clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user