You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
392 lines
10 KiB
C#
392 lines
10 KiB
C#
3 weeks ago
|
using UnityEngine;
|
||
|
|
||
|
namespace Fusion.Addons.SimpleKCC
|
||
|
{
|
||
|
internal sealed class KCCData
|
||
|
{
|
||
|
public int Frame;
|
||
|
|
||
|
public int Tick;
|
||
|
|
||
|
public float Alpha;
|
||
|
|
||
|
public float Time;
|
||
|
|
||
|
public float DeltaTime;
|
||
|
|
||
|
public float UpdateDeltaTime;
|
||
|
|
||
|
public bool IsActive = true;
|
||
|
|
||
|
public Vector3 BasePosition;
|
||
|
|
||
|
public Vector3 DesiredPosition;
|
||
|
|
||
|
public Vector3 TargetPosition;
|
||
|
|
||
|
public Vector3 InputDirection;
|
||
|
|
||
|
public Vector3 JumpImpulse;
|
||
|
|
||
|
public Vector3 Gravity;
|
||
|
|
||
|
public float MaxGroundAngle;
|
||
|
|
||
|
public float MaxWallAngle;
|
||
|
|
||
|
public float MaxHangAngle;
|
||
|
|
||
|
public int MaxPenetrationSteps;
|
||
|
|
||
|
public Vector3 ExternalVelocity;
|
||
|
|
||
|
public Vector3 ExternalAcceleration;
|
||
|
|
||
|
public Vector3 ExternalImpulse;
|
||
|
|
||
|
public Vector3 ExternalForce;
|
||
|
|
||
|
public Vector3 ExternalDelta;
|
||
|
|
||
|
public float KinematicSpeed;
|
||
|
|
||
|
public Vector3 KinematicTangent;
|
||
|
|
||
|
public Vector3 KinematicDirection;
|
||
|
|
||
|
public Vector3 KinematicVelocity;
|
||
|
|
||
|
public Vector3 DynamicVelocity;
|
||
|
|
||
|
public float RealSpeed;
|
||
|
|
||
|
public Vector3 RealVelocity;
|
||
|
|
||
|
public int JumpFrames;
|
||
|
|
||
|
public bool HasTeleported;
|
||
|
|
||
|
public bool IsGrounded;
|
||
|
|
||
|
public bool WasGrounded;
|
||
|
|
||
|
public bool IsSteppingUp;
|
||
|
|
||
|
public bool WasSteppingUp;
|
||
|
|
||
|
public bool IsSnappingToGround;
|
||
|
|
||
|
public bool WasSnappingToGround;
|
||
|
|
||
|
public Vector3 GroundNormal;
|
||
|
|
||
|
public Vector3 GroundTangent;
|
||
|
|
||
|
public Vector3 GroundPosition;
|
||
|
|
||
|
public float GroundDistance;
|
||
|
|
||
|
public float GroundAngle;
|
||
|
|
||
|
public readonly KCCHits Hits = new KCCHits();
|
||
|
|
||
|
private float _lookPitch;
|
||
|
|
||
|
private float _lookYaw;
|
||
|
|
||
|
private Quaternion _lookRotation;
|
||
|
|
||
|
private bool _lookRotationCalculated;
|
||
|
|
||
|
private Vector3 _lookDirection;
|
||
|
|
||
|
private bool _lookDirectionCalculated;
|
||
|
|
||
|
private Quaternion _transformRotation;
|
||
|
|
||
|
private bool _transformRotationCalculated;
|
||
|
|
||
|
private Vector3 _transformDirection;
|
||
|
|
||
|
private bool _transformDirectionCalculated;
|
||
|
|
||
|
public float LookPitch
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _lookPitch;
|
||
|
}
|
||
|
set
|
||
|
{
|
||
|
if (_lookPitch != value)
|
||
|
{
|
||
|
_lookPitch = value;
|
||
|
_lookRotationCalculated = false;
|
||
|
_lookDirectionCalculated = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public float LookYaw
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _lookYaw;
|
||
|
}
|
||
|
set
|
||
|
{
|
||
|
if (_lookYaw != value)
|
||
|
{
|
||
|
_lookYaw = value;
|
||
|
_lookRotationCalculated = false;
|
||
|
_lookDirectionCalculated = false;
|
||
|
_transformRotationCalculated = false;
|
||
|
_transformDirectionCalculated = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Quaternion LookRotation
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (!_lookRotationCalculated)
|
||
|
{
|
||
|
_lookRotation = Quaternion.Euler(_lookPitch, _lookYaw, 0f);
|
||
|
_lookRotationCalculated = true;
|
||
|
}
|
||
|
|
||
|
return _lookRotation;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Vector3 LookDirection
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (!_lookDirectionCalculated)
|
||
|
{
|
||
|
_lookDirection = LookRotation * Vector3.forward;
|
||
|
_lookDirectionCalculated = true;
|
||
|
}
|
||
|
|
||
|
return _lookDirection;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Quaternion TransformRotation
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (!_transformRotationCalculated)
|
||
|
{
|
||
|
_transformRotation = Quaternion.Euler(0f, _lookYaw, 0f);
|
||
|
_transformRotationCalculated = true;
|
||
|
}
|
||
|
|
||
|
return _transformRotation;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Vector3 TransformDirection
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (!_transformDirectionCalculated)
|
||
|
{
|
||
|
_transformDirection = TransformRotation * Vector3.forward;
|
||
|
_transformDirectionCalculated = true;
|
||
|
}
|
||
|
|
||
|
return _transformDirection;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Vector3 DesiredVelocity => KinematicVelocity + DynamicVelocity;
|
||
|
|
||
|
public bool HasJumped => JumpFrames == 1;
|
||
|
|
||
|
public bool IsOnEdge
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
if (!IsGrounded)
|
||
|
{
|
||
|
return WasGrounded;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Vector2 GetLookRotation(bool pitch = true, bool yaw = true)
|
||
|
{
|
||
|
Vector2 result = default(Vector2);
|
||
|
if (pitch)
|
||
|
{
|
||
|
result.x = _lookPitch;
|
||
|
}
|
||
|
|
||
|
if (yaw)
|
||
|
{
|
||
|
result.y = _lookYaw;
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public void AddLookRotation(float pitchDelta, float yawDelta)
|
||
|
{
|
||
|
if (pitchDelta != 0f)
|
||
|
{
|
||
|
LookPitch = Mathf.Clamp(LookPitch + pitchDelta, -90f, 90f);
|
||
|
}
|
||
|
|
||
|
if (yawDelta != 0f)
|
||
|
{
|
||
|
float num;
|
||
|
for (num = LookYaw + yawDelta; num > 180f; num -= 360f)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
for (; num < -180f; num += 360f)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
LookYaw = num;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void AddLookRotation(float pitchDelta, float yawDelta, float minPitch, float maxPitch)
|
||
|
{
|
||
|
if (pitchDelta != 0f)
|
||
|
{
|
||
|
if (minPitch < -90f)
|
||
|
{
|
||
|
minPitch = -90f;
|
||
|
}
|
||
|
|
||
|
if (maxPitch > 90f)
|
||
|
{
|
||
|
maxPitch = 90f;
|
||
|
}
|
||
|
|
||
|
if (maxPitch < minPitch)
|
||
|
{
|
||
|
maxPitch = minPitch;
|
||
|
}
|
||
|
|
||
|
LookPitch = Mathf.Clamp(LookPitch + pitchDelta, minPitch, maxPitch);
|
||
|
}
|
||
|
|
||
|
if (yawDelta != 0f)
|
||
|
{
|
||
|
float num;
|
||
|
for (num = LookYaw + yawDelta; num > 180f; num -= 360f)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
for (; num < -180f; num += 360f)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
LookYaw = num;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void AddLookRotation(Vector2 lookRotationDelta)
|
||
|
{
|
||
|
AddLookRotation(lookRotationDelta.x, lookRotationDelta.y);
|
||
|
}
|
||
|
|
||
|
public void AddLookRotation(Vector2 lookRotationDelta, float minPitch, float maxPitch)
|
||
|
{
|
||
|
AddLookRotation(lookRotationDelta.x, lookRotationDelta.y, minPitch, maxPitch);
|
||
|
}
|
||
|
|
||
|
public void SetLookRotation(float pitch, float yaw)
|
||
|
{
|
||
|
KCCUtility.ClampLookRotationAngles(ref pitch, ref yaw);
|
||
|
LookPitch = pitch;
|
||
|
LookYaw = yaw;
|
||
|
}
|
||
|
|
||
|
public void SetLookRotation(Quaternion lookRotation, bool preservePitch = false, bool preserveYaw = false)
|
||
|
{
|
||
|
KCCUtility.GetClampedLookRotationAngles(lookRotation, out var pitch, out var yaw);
|
||
|
if (!preservePitch)
|
||
|
{
|
||
|
LookPitch = pitch;
|
||
|
}
|
||
|
|
||
|
if (!preserveYaw)
|
||
|
{
|
||
|
LookYaw = yaw;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void ClearTransientProperties()
|
||
|
{
|
||
|
JumpFrames = 0;
|
||
|
JumpImpulse = default(Vector3);
|
||
|
ExternalVelocity = default(Vector3);
|
||
|
ExternalAcceleration = default(Vector3);
|
||
|
ExternalImpulse = default(Vector3);
|
||
|
ExternalForce = default(Vector3);
|
||
|
}
|
||
|
|
||
|
public void Clear()
|
||
|
{
|
||
|
Hits.Clear();
|
||
|
}
|
||
|
|
||
|
public void CopyFromOther(KCCData other)
|
||
|
{
|
||
|
Frame = other.Frame;
|
||
|
Tick = other.Tick;
|
||
|
Alpha = other.Alpha;
|
||
|
Time = other.Time;
|
||
|
DeltaTime = other.DeltaTime;
|
||
|
UpdateDeltaTime = other.UpdateDeltaTime;
|
||
|
IsActive = other.IsActive;
|
||
|
BasePosition = other.BasePosition;
|
||
|
DesiredPosition = other.DesiredPosition;
|
||
|
TargetPosition = other.TargetPosition;
|
||
|
LookPitch = other.LookPitch;
|
||
|
LookYaw = other.LookYaw;
|
||
|
InputDirection = other.InputDirection;
|
||
|
JumpImpulse = other.JumpImpulse;
|
||
|
Gravity = other.Gravity;
|
||
|
MaxGroundAngle = other.MaxGroundAngle;
|
||
|
MaxWallAngle = other.MaxWallAngle;
|
||
|
MaxHangAngle = other.MaxHangAngle;
|
||
|
MaxPenetrationSteps = other.MaxPenetrationSteps;
|
||
|
ExternalVelocity = other.ExternalVelocity;
|
||
|
ExternalAcceleration = other.ExternalAcceleration;
|
||
|
ExternalImpulse = other.ExternalImpulse;
|
||
|
ExternalForce = other.ExternalForce;
|
||
|
ExternalDelta = other.ExternalDelta;
|
||
|
KinematicSpeed = other.KinematicSpeed;
|
||
|
KinematicTangent = other.KinematicTangent;
|
||
|
KinematicDirection = other.KinematicDirection;
|
||
|
KinematicVelocity = other.KinematicVelocity;
|
||
|
DynamicVelocity = other.DynamicVelocity;
|
||
|
RealSpeed = other.RealSpeed;
|
||
|
RealVelocity = other.RealVelocity;
|
||
|
JumpFrames = other.JumpFrames;
|
||
|
HasTeleported = other.HasTeleported;
|
||
|
IsGrounded = other.IsGrounded;
|
||
|
WasGrounded = other.WasGrounded;
|
||
|
IsSteppingUp = other.IsSteppingUp;
|
||
|
WasSteppingUp = other.WasSteppingUp;
|
||
|
IsSnappingToGround = other.IsSnappingToGround;
|
||
|
WasSnappingToGround = other.WasSnappingToGround;
|
||
|
GroundNormal = other.GroundNormal;
|
||
|
GroundTangent = other.GroundTangent;
|
||
|
GroundPosition = other.GroundPosition;
|
||
|
GroundDistance = other.GroundDistance;
|
||
|
GroundAngle = other.GroundAngle;
|
||
|
Hits.CopyFromOther(other.Hits);
|
||
|
}
|
||
|
}
|
||
|
}
|