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 Object sourceObject();
|
||||||
// public
|
// public
|
||||||
// }
|
// }
|
||||||
public class UnitMovementData{
|
|
||||||
|
[Serializable]
|
||||||
|
public class UnitMovementData : ICloneable{
|
||||||
// Movement Direction
|
// Movement Direction
|
||||||
public float accelerationSmoothing = 5f;
|
public float accelerationSmoothing = 5f;
|
||||||
public float deaccelerationSmoothing = 5f;
|
public float deaccelerationSmoothing = 5f;
|
||||||
@@ -30,21 +32,26 @@ namespace Reset.Units{
|
|||||||
|
|
||||||
// Jumping
|
// Jumping
|
||||||
[ShowInInspector, ReadOnly] public float jumpPower;
|
[ShowInInspector, ReadOnly] public float jumpPower;
|
||||||
public float jumpPowerDecay;
|
public float jumpPowerDecay = 3f;
|
||||||
|
|
||||||
// Gravity
|
// Gravity
|
||||||
[ShowInInspector, ReadOnly] public float gravityPower;
|
[ShowInInspector, ReadOnly] public float gravityPower;
|
||||||
public float gravityMax;
|
public float gravityMax = 8f;
|
||||||
public float gravityAcceleration;
|
public float gravityAcceleration = 1f;
|
||||||
|
public float gravityScale = 1f;
|
||||||
|
|
||||||
// Rotation
|
// Rotation
|
||||||
public PlayerFacingDirection rotateFacing;
|
public PlayerFacingDirection rotateFacing;
|
||||||
public float rotationSpeedTarget = 5f;
|
public float rotationSpeedTarget = 5f;
|
||||||
public float rotationSmoothing;
|
public float rotationSmoothing = 1f;
|
||||||
|
|
||||||
// Smoothing
|
// Smoothing
|
||||||
public Quaternion targetRotation;
|
public Quaternion targetRotation;
|
||||||
public float currentRotSpeed;
|
public float currentRotSpeed;
|
||||||
|
|
||||||
|
public object Clone(){
|
||||||
|
return this.MemberwiseClone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UnitMovementHandler : MonoBehaviour{
|
public class UnitMovementHandler : MonoBehaviour{
|
||||||
@@ -63,9 +70,14 @@ namespace Reset.Units{
|
|||||||
private CharacterController controller;
|
private CharacterController controller;
|
||||||
public PlayerControls controls;
|
public PlayerControls controls;
|
||||||
|
|
||||||
[SerializeReference, ShowInInspector]
|
[ShowInInspector, PropertyOrder(2)]
|
||||||
public UnitMovementData data = new UnitMovementData();
|
public UnitMovementData data = new UnitMovementData();
|
||||||
|
|
||||||
|
[Button, PropertyOrder(1)]
|
||||||
|
void ResetMovementData(){
|
||||||
|
data = new UnitMovementData();
|
||||||
|
}
|
||||||
|
|
||||||
void Awake(){
|
void Awake(){
|
||||||
controller = GetComponent<CharacterController>();
|
controller = GetComponent<CharacterController>();
|
||||||
}
|
}
|
||||||
@@ -81,13 +93,7 @@ namespace Reset.Units{
|
|||||||
|
|
||||||
private void UpdateCurrentDirection(){
|
private void UpdateCurrentDirection(){
|
||||||
// Get input value
|
// Get input value
|
||||||
Vector3 inputMovement =
|
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
|
||||||
new Vector3(
|
|
||||||
controls.
|
|
||||||
rawMoveInput.x,
|
|
||||||
0f,
|
|
||||||
controls.
|
|
||||||
rawMoveInput.y);
|
|
||||||
|
|
||||||
// Construct move direction
|
// Construct move direction
|
||||||
Vector3 targetDirection = inputMovement;
|
Vector3 targetDirection = inputMovement;
|
||||||
@@ -105,7 +111,7 @@ namespace Reset.Units{
|
|||||||
currentNoY = Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime);
|
currentNoY = Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
|
||||||
|
|
||||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime);
|
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime);
|
||||||
}
|
}
|
||||||
@@ -129,16 +135,13 @@ namespace Reset.Units{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create the final gravity value
|
// 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
|
// 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(){
|
private void UpdateCurrentRotation(){
|
||||||
// Calculate rotation speed
|
|
||||||
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeedTarget, data.rotationSmoothing * Time.deltaTime);
|
|
||||||
|
|
||||||
// Get input value
|
// 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);
|
||||||
|
|
||||||
@@ -166,17 +169,28 @@ namespace Reset.Units{
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate rotation speed
|
||||||
|
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeedTarget, data.rotationSmoothing * Time.deltaTime);
|
||||||
|
|
||||||
// Set final rotation
|
// 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(){
|
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
|
// 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(){
|
public void LateUpdate(){
|
||||||
@@ -185,6 +199,11 @@ namespace Reset.Units{
|
|||||||
|
|
||||||
private void UpdateGravityLate(){
|
private void UpdateGravityLate(){
|
||||||
// Decay jump power
|
// 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