Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed Jul 5, 2024
1 parent 91fbeeb commit dba73e4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ AnimatorStateTransition:
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.06615287
m_TransitionDuration: 0.09497708
m_TransitionOffset: 0
m_ExitTime: 0.58234453
m_ExitTime: 0.5823446
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
Expand Down Expand Up @@ -615,9 +615,9 @@ AnimatorStateTransition:
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionOffset: 0
m_ExitTime: 0.9
m_TransitionDuration: 0.13899994
m_TransitionOffset: 0.17746145
m_ExitTime: 0.9101155
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
Expand Down Expand Up @@ -646,9 +646,9 @@ AnimatorStateTransition:
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionDuration: 0.12810051
m_TransitionOffset: 0
m_ExitTime: 0.5614035
m_ExitTime: 0.56140345
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Unity.Cinemachine.Samples
/// and expects to be driven by the SimplePlayerControllerBase using the STartJump, EndJump,
/// and PostUpdate callbacks.
/// </summary>
[RequireComponent(typeof(Animator))]
public class SimplePlayerAnimator : MonoBehaviour
{
[Tooltip("Tune this to the animation in the model: feet should not slide when walking at this speed")]
Expand All @@ -34,23 +33,39 @@ public class SimplePlayerAnimator : MonoBehaviour
[Tooltip("Scale factor for the overall speed of the jump animation")]
public float JumpAnimationScale = 0.65f;

Animator m_Animator;
SimplePlayerControllerBase m_Controller;
Vector3 m_PreviousPosition; // used if m_Controller == null or disabled
bool m_WasWalking;
bool m_WasRunning;
bool m_IsWalking;
bool m_IsRunning;
bool m_IsJumping;
bool m_LandTriggered;
bool m_JumpTriggered;
const float k_IdleThreshold = 0.2f;

public enum States { Idle, Walk, Run, Jump, RunJump }

/// <summary>Current state of the player</summary>
public States State
{
get
{
if (m_IsJumping)
return m_IsRunning ? States.RunJump : States.Jump;
if (m_IsRunning)
return States.Run;
return m_IsWalking ? States.Walk : States.Idle;
}
}

void Start()
{
m_PreviousPosition = transform.position;
TryGetComponent(out m_Animator);
m_Controller = GetComponentInParent<SimplePlayerControllerBase>();
if (m_Controller != null)
{
// Install our callbacks to handle jump and animation based on velocity
m_Controller.StartJump += () => OnJump(true);
m_Controller.EndJump += () => OnJump(false);
m_Controller.StartJump += () => m_JumpTriggered = true;
m_Controller.EndJump += () => m_LandTriggered = true;
m_Controller.PostUpdate += (vel, jumpAnimationScale) => UpdateAnimation(vel, jumpAnimationScale);
}
}
Expand All @@ -73,13 +88,6 @@ virtual protected void LateUpdate()
}
}

/// <summary>
/// Called by the SimplePlayerControllerBase when the player starts or ends a jump.
/// Override this to interact appropriately with your animation controller.
/// </summary>
/// <param name="jumping">True when jump starts, false when it ends.</param>
virtual protected void OnJump(bool jumping) => m_Animator.SetTrigger(jumping ? "Jump" : "Land");

/// <summary>
/// Update the animation based on the player's velocity.
/// Override this to interact appropriately with your animation controller.
Expand All @@ -89,14 +97,20 @@ virtual protected void LateUpdate()
/// It can be used to slow down the jump animation for longer jumps.</param>
virtual protected void UpdateAnimation(Vector3 vel, float jumpAnimationScale)
{
if (!TryGetComponent(out Animator animator))
{
Debug.LogError("SimplePlayerAnimator: An Animator component is required");
return;
}

vel.y = 0; // we don't consider vertical movement
var speed = vel.magnitude;

// Hysteresis reduction
bool isRunning = speed > NormalWalkSpeed * 2 + (m_WasRunning ? -0.15f : 0.15f);
bool isWalking = !isRunning && speed > k_IdleThreshold + (m_WasWalking ? -0.05f : 0.05f);
m_WasWalking = isWalking;
m_WasRunning = isRunning;
bool isRunning = speed > NormalWalkSpeed * 2 + (m_IsRunning ? -0.15f : 0.15f);
bool isWalking = !isRunning && speed > k_IdleThreshold + (m_IsWalking ? -0.05f : 0.05f);
m_IsWalking = isWalking;
m_IsRunning = isRunning;

// Set the normalized direction of motion and scale the animation speed to match motion speed
var dir = speed > k_IdleThreshold ? vel / speed : Vector3.zero;
Expand All @@ -109,12 +123,25 @@ virtual protected void UpdateAnimation(Vector3 vel, float jumpAnimationScale)
? speed / NormalSprintSpeed
: Mathf.Min(MaxSprintScale, 1 + (speed - NormalSprintSpeed) / (3 * NormalSprintSpeed));

m_Animator.SetFloat("DirX", dir.x);
m_Animator.SetFloat("DirZ", dir.z);
m_Animator.SetFloat("MotionScale", motionScale);
m_Animator.SetBool("Walking", isWalking);
m_Animator.SetBool("Running", isRunning);
m_Animator.SetFloat("JumpScale", JumpAnimationScale * jumpAnimationScale);
animator.SetFloat("DirX", dir.x);
animator.SetFloat("DirZ", dir.z);
animator.SetFloat("MotionScale", motionScale);
animator.SetBool("Walking", isWalking);
animator.SetBool("Running", isRunning);
animator.SetFloat("JumpScale", JumpAnimationScale * jumpAnimationScale);

if (m_JumpTriggered)
{
animator.SetTrigger("Jump");
m_JumpTriggered = false;
m_IsJumping = true;
}
if (m_LandTriggered)
{
animator.SetTrigger("Land");
m_LandTriggered = false;
m_IsJumping = false;
}
}
}
}

0 comments on commit dba73e4

Please sign in to comment.