fix: various changes to handler such as gravity speed decoupling, data cloning, rotation smoothing,
This commit is contained in:
@@ -18,7 +18,9 @@ namespace Reset.Units{
|
||||
// public Object sourceObject();
|
||||
// public
|
||||
// }
|
||||
public class UnitMovementData{
|
||||
|
||||
[Serializable]
|
||||
public class UnitMovementData : ICloneable{
|
||||
// Movement Direction
|
||||
public float accelerationSmoothing = 5f;
|
||||
public float deaccelerationSmoothing = 5f;
|
||||
@@ -30,21 +32,26 @@ namespace Reset.Units{
|
||||
|
||||
// Jumping
|
||||
[ShowInInspector, ReadOnly] public float jumpPower;
|
||||
public float jumpPowerDecay;
|
||||
public float jumpPowerDecay = 3f;
|
||||
|
||||
// Gravity
|
||||
[ShowInInspector, ReadOnly] public float gravityPower;
|
||||
public float gravityMax;
|
||||
public float gravityAcceleration;
|
||||
public float gravityMax = 8f;
|
||||
public float gravityAcceleration = 1f;
|
||||
public float gravityScale = 1f;
|
||||
|
||||
// Rotation
|
||||
public PlayerFacingDirection rotateFacing;
|
||||
public float rotationSpeedTarget = 5f;
|
||||
public float rotationSmoothing;
|
||||
public float rotationSmoothing = 1f;
|
||||
|
||||
// Smoothing
|
||||
public Quaternion targetRotation;
|
||||
public float currentRotSpeed;
|
||||
|
||||
public object Clone(){
|
||||
return this.MemberwiseClone();
|
||||
}
|
||||
}
|
||||
|
||||
public class UnitMovementHandler : MonoBehaviour{
|
||||
@@ -63,8 +70,13 @@ namespace Reset.Units{
|
||||
private CharacterController controller;
|
||||
public PlayerControls controls;
|
||||
|
||||
[SerializeReference, ShowInInspector]
|
||||
[ShowInInspector, PropertyOrder(2)]
|
||||
public UnitMovementData data = new UnitMovementData();
|
||||
|
||||
[Button, PropertyOrder(1)]
|
||||
void ResetMovementData(){
|
||||
data = new UnitMovementData();
|
||||
}
|
||||
|
||||
void Awake(){
|
||||
controller = GetComponent<CharacterController>();
|
||||
@@ -81,13 +93,7 @@ namespace Reset.Units{
|
||||
|
||||
private void UpdateCurrentDirection(){
|
||||
// Get input value
|
||||
Vector3 inputMovement =
|
||||
new Vector3(
|
||||
controls.
|
||||
rawMoveInput.x,
|
||||
0f,
|
||||
controls.
|
||||
rawMoveInput.y);
|
||||
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
|
||||
|
||||
// Construct move direction
|
||||
Vector3 targetDirection = inputMovement;
|
||||
@@ -105,7 +111,7 @@ namespace Reset.Units{
|
||||
currentNoY = Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime);
|
||||
|
||||
} else {
|
||||
float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
||||
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
||||
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime);
|
||||
}
|
||||
@@ -129,16 +135,13 @@ namespace Reset.Units{
|
||||
}
|
||||
|
||||
// Create the final gravity value
|
||||
float gravityMoveDirection = data.jumpPower + Physics.gravity.y * data.gravityPower;
|
||||
float gravityMoveDirection = data.jumpPower + (Physics.gravity.y * data.gravityPower);
|
||||
|
||||
// Commit gravity to move direction, ignoring XZ since those are done in UpdateMovementDirection
|
||||
outputMoveDirection = new Vector3(outputMoveDirection.x, gravityMoveDirection, outputMoveDirection.z);
|
||||
outputMoveDirection.y = gravityMoveDirection;
|
||||
}
|
||||
|
||||
private void UpdateCurrentRotation(){
|
||||
// Calculate rotation speed
|
||||
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeedTarget, data.rotationSmoothing * Time.deltaTime);
|
||||
|
||||
// Get input value
|
||||
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
|
||||
|
||||
@@ -165,18 +168,29 @@ namespace Reset.Units{
|
||||
outputRotation = transform.rotation;
|
||||
break;
|
||||
}
|
||||
|
||||
// Calculate rotation speed
|
||||
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeedTarget, data.rotationSmoothing * Time.deltaTime);
|
||||
|
||||
// Set final rotation
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, outputRotation, 10f * Time.deltaTime).Flatten(0, null, 0);
|
||||
transform.rotation = Quaternion.Lerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0);
|
||||
}
|
||||
|
||||
public void DoMovement(){
|
||||
DoMovement(outputMoveDirection, outputSpeed);
|
||||
DoMovement(outputMoveDirection, outputSpeed, data.gravityScale);
|
||||
}
|
||||
|
||||
public void DoMovement(Vector3 moveDir, float speed){
|
||||
public void DoMovement(Vector3 moveDir, float speed, float gravity){
|
||||
// Commit the move, with respect to the camera's rotation
|
||||
controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * (moveDir * speed) * Time.deltaTime));
|
||||
Vector3 moveXZDir = new Vector3(moveDir.x, 0f, moveDir.z);
|
||||
Vector3 moveYDir = new Vector3(0f, moveDir.y, 0f);
|
||||
|
||||
moveXZDir *= speed * Time.deltaTime;
|
||||
moveYDir *= gravity * Time.deltaTime;
|
||||
|
||||
Vector3 finalDir = moveXZDir + moveYDir;
|
||||
|
||||
controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * finalDir));
|
||||
}
|
||||
|
||||
public void LateUpdate(){
|
||||
@@ -185,6 +199,11 @@ namespace Reset.Units{
|
||||
|
||||
private void UpdateGravityLate(){
|
||||
// Decay jump power
|
||||
data.jumpPower -= data.jumpPowerDecay * Time.deltaTime;
|
||||
}
|
||||
|
||||
public void AssignNewData(UnitMovementData movementData){
|
||||
data = (UnitMovementData)movementData.Clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user