PlayerMove - Animation

This commit is contained in:
MrGadget1024 2023-02-25 21:30:23 -05:00
parent b0b039f468
commit 214dc43750

View File

@ -1,18 +1,20 @@
using UnityEngine; using UnityEngine;
using Mirror; using Mirror;
using System;
namespace TestNT namespace TestNT
{ {
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Rigidbody))]
public class PlayerMove : NetworkBehaviour public class PlayerMove : NetworkBehaviour
{ {
public enum GroundState : byte { Jumping, Falling, Grounded } public enum GroundState : byte { Jumping, Falling, Grounded }
public enum MoveMode : byte { Walking, Sneaking, Running };
[Header("Avatar Components")] [Header("Avatar Components")]
public CharacterController characterController;
public NTRCustomSendInterval NTR; public NTRCustomSendInterval NTR;
public Animator animator; public Animator animator;
public CharacterController characterController;
[Header("Materials")] [Header("Materials")]
public PhysicMaterial physicsMaterial; public PhysicMaterial physicsMaterial;
@ -37,6 +39,7 @@ public enum GroundState : byte { Jumping, Falling, Grounded }
[Header("Diagnostics - Do Not Modify")] [Header("Diagnostics - Do Not Modify")]
public GroundState groundState = GroundState.Grounded; public GroundState groundState = GroundState.Grounded;
public MoveMode moveState = MoveMode.Running;
[Range(-1f, 1f)] [Range(-1f, 1f)]
public float horizontal; public float horizontal;
@ -63,12 +66,12 @@ void OnValidate()
if (NTR == null) if (NTR == null)
NTR = GetComponentInChildren<NTRCustomSendInterval>(); NTR = GetComponentInChildren<NTRCustomSendInterval>();
if (characterController == null)
characterController = GetComponent<CharacterController>();
if (animator == null) if (animator == null)
animator = GetComponentInChildren<Animator>(); animator = GetComponentInChildren<Animator>();
if (characterController == null)
characterController = GetComponent<CharacterController>();
// Override CharacterController default values // Override CharacterController default values
characterController.enabled = false; characterController.enabled = false;
characterController.skinWidth = 0.02f; characterController.skinWidth = 0.02f;
@ -80,6 +83,17 @@ void OnValidate()
this.enabled = false; this.enabled = false;
} }
public override void OnStartClient()
{
if (!isLocalPlayer)
NTR.VelRotChangedAction = OnVelRotChanged;
}
public override void OnStopClient()
{
NTR.VelRotChangedAction = null;
}
public override void OnStartAuthority() public override void OnStartAuthority()
{ {
characterController.enabled = true; characterController.enabled = true;
@ -104,10 +118,21 @@ void Update()
return; return;
HandleTeleport(); HandleTeleport();
#if !UNITY_SERVER
// Not needed on headless clients
HandleMoveState();
#endif
HandleTurning(); HandleTurning();
HandleJumping(); HandleJumping();
HandleMove(); HandleMove();
#if !UNITY_SERVER
// Not needed on headless clients
HandleAnimation();
#endif
// Reset ground state // Reset ground state
if (characterController.isGrounded) if (characterController.isGrounded)
groundState = GroundState.Grounded; groundState = GroundState.Grounded;
@ -125,7 +150,7 @@ void HandleTeleport()
{ {
NTR.CmdTeleport(Vector3.up); NTR.CmdTeleport(Vector3.up);
} }
else else
{ {
characterController.enabled = false; characterController.enabled = false;
transform.position = Vector3.up; transform.position = Vector3.up;
@ -133,6 +158,23 @@ void HandleTeleport()
} }
} }
// Headless clients don't need to do either of these
#if !UNITY_SERVER
void HandleMoveState()
{
if (Input.GetKeyUp(KeyCode.R) && moveState == MoveMode.Walking)
moveState = MoveMode.Running;
else if (Input.GetKeyUp(KeyCode.R) && moveState == MoveMode.Running)
moveState = MoveMode.Walking;
else if (Input.GetKeyUp(KeyCode.C) && moveState != MoveMode.Sneaking)
moveState = MoveMode.Sneaking;
else if (Input.GetKeyUp(KeyCode.C) && moveState == MoveMode.Sneaking)
moveState = MoveMode.Walking;
}
#endif
// Alternative methods provided for headless clients to act autonomously // Alternative methods provided for headless clients to act autonomously
#if !UNITY_SERVER #if !UNITY_SERVER
@ -153,6 +195,9 @@ void HandleTurning()
if (!Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E)) if (!Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E))
turnSpeed = Mathf.MoveTowards(turnSpeed, 0, turnDelta); turnSpeed = Mathf.MoveTowards(turnSpeed, 0, turnDelta);
if (moveState == MoveMode.Sneaking)
turnSpeed /= 3;
transform.Rotate(0f, turnSpeed * Time.deltaTime, 0f); transform.Rotate(0f, turnSpeed * Time.deltaTime, 0f);
} }
@ -163,7 +208,7 @@ void HandleJumping()
// as player holds spacebar. Jump power is increased by a diminishing amout // as player holds spacebar. Jump power is increased by a diminishing amout
// every frame until it reaches maxJumpSpeed, or player releases the spacebar, // every frame until it reaches maxJumpSpeed, or player releases the spacebar,
// and then changes to the falling state until it gets grounded. // and then changes to the falling state until it gets grounded.
if (groundState != GroundState.Falling && Input.GetKey(KeyCode.Space)) if (groundState != GroundState.Falling && moveState != MoveMode.Sneaking && Input.GetKey(KeyCode.Space))
{ {
if (groundState != GroundState.Jumping) if (groundState != GroundState.Jumping)
{ {
@ -223,6 +268,12 @@ void HandleMove()
// Create initial direction vector without jumpSpeed (y-axis). // Create initial direction vector without jumpSpeed (y-axis).
direction = new Vector3(horizontal, 0f, vertical); direction = new Vector3(horizontal, 0f, vertical);
// Run unless Sneaking or Walking
if (moveState == MoveMode.Sneaking)
direction *= 0.15f;
else if (moveState == MoveMode.Walking)
direction *= 0.5f;
// Clamp so diagonal strafing isn't a speed advantage. // Clamp so diagonal strafing isn't a speed advantage.
direction = Vector3.ClampMagnitude(direction, 1f); direction = Vector3.ClampMagnitude(direction, 1f);
@ -238,5 +289,44 @@ void HandleMove()
// Finally move the character. // Finally move the character.
characterController.Move(direction * Time.deltaTime); characterController.Move(direction * Time.deltaTime);
} }
void HandleAnimation()
{
if (!animator) return;
//if (moveState != MoveState.Sneaking)
//{
// if (Input.GetKeyUp(KeyCode.I))
// animator.SetTrigger("Saluting");
// else if (Input.GetKeyUp(KeyCode.O))
// animator.SetTrigger("Waving");
// else if (Input.GetKeyUp(KeyCode.P))
// animator.SetBool("Dancing", !animator.GetBool("Dancing"));
//}
animVelocity = transform.InverseTransformDirection(direction).z / moveSpeedMultiplier;
animRotation = turnSpeed / maxTurnSpeed;
animator.SetFloat("Forward", Mathf.MoveTowards(animator.GetFloat("Forward"), animVelocity, moveSpeedMultiplier * Time.deltaTime));
animator.SetFloat("Turn", Mathf.MoveTowards(animator.GetFloat("Turn"), animRotation, maxTurnSpeed * Time.deltaTime));
animator.SetBool("Crouch", moveState == MoveMode.Sneaking);
animator.SetBool("OnGround", groundState == GroundState.Grounded);
}
void OnVelRotChanged(Vector3 newVelocity, Vector3 newRotation)
{
// Only apply to other player objects
if (isLocalPlayer) return;
animVelocity = -MathF.Round(transform.InverseTransformDirection(newVelocity).z / moveSpeedMultiplier, 1);
animRotation = -MathF.Round(newRotation.y / maxTurnSpeed, 1);
if (animator)
{
animator.SetFloat("Forward", MathF.Round(Mathf.MoveTowards(animator.GetFloat("Forward"), animVelocity, moveSpeedMultiplier * Time.deltaTime), 1));
animator.SetFloat("Turn", MathF.Round(Mathf.MoveTowards(animator.GetFloat("Turn"), animRotation, maxTurnSpeed * Time.deltaTime), 1));
}
}
} }
} }