@@ -1,4 +1,5 @@
using RebootKit.Engine.Extensions ;
using System ;
using RebootKit.Engine.Extensions ;
using RebootKit.Engine.Foundation ;
using RebootKit.Engine.Services.Simulation ;
using Unity.Mathematics ;
@@ -7,7 +8,7 @@ using Logger = RebootKit.Engine.Foundation.Logger;
namespace RebootReality.jelycho.Player {
public class PlayerActor : Actor {
static readonly Logger s_l ogger = new Logger ( nameof ( PlayerActor ) ) ;
static readonly Logger s_L ogger = new Logger ( nameof ( PlayerActor ) ) ;
[SerializeField] Animator m_Animator ;
@@ -17,7 +18,6 @@ namespace RebootReality.jelycho.Player {
[Header("Camera")]
[SerializeField] FPPCamera m_Camera ;
[SerializeField] CameraSpring m_CameraSpring ;
[SerializeField] Transform m_HeadBoneTransform ;
[SerializeField, Range(0.0f, 1.0f), Tooltip("Percentage of run speed")]
float m_EnableCameraBobbingPercentThreshold = 0.5f ;
@@ -29,23 +29,44 @@ namespace RebootReality.jelycho.Player {
float m_TargetCameraBobbing = 0.0f ;
float m_CurrentCameraBobbing = 0.0f ;
[Header("Character")]
[SerializeField] Transform m_CharacterRootTransform ;
[SerializeField] Transform m_HeadBoneTransform ;
[SerializeField] Transform m_HeadAimTargetTransform ;
[SerializeField] Transform m_CharacterForwardTransform ;
[SerializeField, Range(0.0f, 90.0f)] float m_CharacterRotateDeadAngle = 5.0f ;
[SerializeField, Range(0.0f, 90.0f)] float m_CharacterRotateSoftAngle = 90.0f ;
[SerializeField] float m_CharacterRotateSpeed = 180.0f ;
[SerializeField] float m_CharacterRotateFastSpeed = 720.0f ;
float m_CharacterTurnVelocity = 0.0f ;
[Header("Dragging")]
[SerializeField] Transform m_DragGutStartPosition ;
[SerializeField] PhysicsObjectDragger m_PhysicsDragger ;
[SerializeField] FloatRange m_DragDistanceRange = new FloatRange ( 1.0f , 5.0f ) ;
[SerializeField] LineRenderer m_LineRenderer ;
public float3 LookDirection {
get {
float pitchRad = math . radians ( - m_Camera . Pitch ) ;
float yawRad = math . radians ( m_Camera . Yaw ) ;
return new float3 ( math . sin ( yawRad ) * math . cos ( pitchRad ) ,
math . sin ( pitchRad ) ,
math . cos ( yawRad ) * math . cos ( pitchRad ) ) ;
}
}
public override void OnBeginPlay ( ) {
m_CameraSpring . Initialize ( ) ;
}
public override void OnTick ( ) {
public override void OnTick ( float deltaTime ) {
// Camera Stuff
m_Camera . Tick ( ) ;
m_Locomotion . YawRotation = m_Camera . Yaw ;
// Camera bobbing
if ( m_Locomotion . IsGrounded & &
m_Locomotion . SpeedXZ > = m_Locomotion . runSpeed * m_EnableCameraBobbingPercentThreshold ) {
if ( m_Locomotion . IsSprinting ) {
@@ -62,22 +83,50 @@ namespace RebootReality.jelycho.Player {
m_CameraBobbingTransitionSpeed * Time . deltaTime ) ;
m_Camera . SetBobbing ( m_CurrentCameraBobbing ) ;
m_PhysicsDragger . TargetWorldPosition = m_Camera . Camera . transform . position +
m_Camera . Camera . transform . forward * 2.0f ;
if ( m_PhysicsDragger . Current . OrNull ( ) is not null ) {
m_LineRenderer . enabled = true ;
m_LineRenderer . SetPosition ( 0 , m_LineRenderer . transform . InverseTransformPoint ( m_DragGutStartPosition . position ) ) ;
m_LineRenderer . SetPosition ( 1 , m_LineRenderer . transform . InverseTransformPoint ( m_PhysicsDragger . Current . position ) ) ;
} else {
m_LineRenderer . enabled = false ;
}
Vector3 actorUp = transform . up ;
Vector3 actorRight = transform . right ;
Vector3 actorForward = transform . forward ;
m_CameraSpring . UpdateSpring ( Time . deltaTime , actorUp , actorRight , actorForward ) ;
// Character rotation
float3 targetCharacterForward = math . normalize ( LookDirection . With ( y : 0.0f ) ) ;
float3 currentCharacterForward = math . normalize ( m_CharacterForwardTransform . forward . With ( y : 0.0f ) ) ;
float angleRad = math . acos ( math . clamp ( math . dot ( targetCharacterForward , currentCharacterForward ) / ( math . length ( targetCharacterForward ) * math . length ( currentCharacterForward ) ) , - 1f , 1f ) ) ;
float angleDeg = math . degrees ( angleRad ) ;
bool rotateCharacter = false ;
float rotateCharacterSpeed = m_CharacterRotateSpeed ;
m_CharacterTurnVelocity = 0.0f ;
if ( math . abs ( angleDeg ) > m_CharacterRotateDeadAngle ) {
if ( math . abs ( angleDeg ) < m_CharacterRotateSoftAngle ) {
rotateCharacter = true ;
} else {
rotateCharacter = true ;
rotateCharacterSpeed = m_CharacterRotateFastSpeed ;
}
}
float velocityForward = m_Locomotion . Velocity . z ;
if ( ! rotateCharacter & & math . abs ( velocityForward ) > 0.01f ) {
rotateCharacter = true ;
}
if ( rotateCharacter ) {
m_CharacterTurnVelocity = rotateCharacterSpeed * deltaTime ;
Vector3 newForward = Vector3 . RotateTowards ( currentCharacterForward ,
targetCharacterForward ,
math . radians ( m_CharacterTurnVelocity ) ,
0.0f ) ;
m_CharacterForwardTransform . forward = newForward ;
}
// Aim Target adjustment
m_HeadAimTargetTransform . position = ( float3 ) m_HeadBoneTransform . position + LookDirection * 5.0f ;
UpdateAnimator ( ) ;
}
@@ -98,7 +147,7 @@ namespace RebootReality.jelycho.Player {
}
public void SetMoveInput ( Vector2 input ) {
float3 direction = Quaternion . AngleAxis ( transform . eulerAngles . y , Vector3 . up ) *
float3 direction = Quaternion . AngleAxis ( m_Camera . Yaw , Vector3 . up ) *
new float3 ( input . x , 0.0f , input . y ) ;
m_Locomotion . SetWishDirection ( direction ) ;
}
@@ -115,19 +164,29 @@ namespace RebootReality.jelycho.Player {
}
struct AnimatorParamHashes {
public static readonly int s_ VelocityForwardNormalized = Animator . StringToHash ( "VelocityForwardNormalized" ) ;
public static readonly int s_ VelocityRightNormalized = Animator . StringToHash ( "VelocityRightNormalized" ) ;
public static readonly int s_IsGrounded = Animator . StringToHash ( "IsGrounded " ) ;
public static readonly int VelocityForwardNormalized = Animator . StringToHash ( "VelocityForwardNormalized" ) ;
public static readonly int VelocityRightNormalized = Animator . StringToHash ( "VelocityRightNormalized" ) ;
public static readonly int TurnVelocity = Animator . StringToHash ( "TurnVelocity " ) ;
public static readonly int IsGrounded = Animator . StringToHash ( "IsGrounded" ) ;
}
void UpdateAnimator ( ) {
Vector3 localVelocity = m_Locomo tion . Local Velocity ;
Vector3 localVelocity = m_CharacterForwardTransform . InverseTransformDirec tion ( m_ Locomotion . Velocity) ;
float forwardNormalized = localVelocity . z / m_Locomotion . runSpeed ;
float rightNormalized = localVelocity . x / m_Locomotion . runSpeed ;
m_Animator . SetFloat ( AnimatorParamHashes . s_VelocityForwardNormalized , forwardNormalized ) ;
m_Animator . SetFloat ( Animat orP aramHashes . s_VelocityRight Normalized, 0.0f ) ;
float turnVelocity = m_CharacterTurnVelocity ;
if ( math . abs ( f orw ard Normalized) > 0.01 f | |
math . abs ( rightNormalized ) > 0.01f | |
! m_Locomotion . IsGrounded ) {
turnVelocity = 0.0f ;
}
m_Animator . SetFloat ( AnimatorParamHashes . VelocityForwardNormalized , forwardNormalized ) ;
m_Animator . SetFloat ( AnimatorParamHashes . VelocityRightNormalized , rightNormalized ) ;
m_Animator . SetFloat ( AnimatorParamHashes . TurnVelocity , turnVelocity ) ;
m_Animator . SetBool ( AnimatorParamHashes . s_ IsGrounded, m_Locomotion . IsGrounded ) ;
m_Animator . SetBool ( AnimatorParamHashes . IsGrounded , m_Locomotion . IsGrounded ) ;
}
}
}