Guest User

ThidPersonCharacter

a guest
Apr 20th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace UnityStandardAssets.Characters.ThirdPerson
  4. {
  5.     [RequireComponent(typeof(Rigidbody))]
  6.     [RequireComponent(typeof(CapsuleCollider))]
  7.     [RequireComponent(typeof(Animator))]
  8.     public class ThirdPersonCharacter : MonoBehaviour
  9.     {
  10.  
  11.         [SerializeField] float m_MovingTurnSpeed = 360;
  12.         [SerializeField] float m_StationaryTurnSpeed = 180;
  13.         Rigidbody m_Rigidbody;
  14.         Animator m_Animator;
  15.         const float k_Half = 0.5f;
  16.         float m_TurnAmount;
  17.         float m_ForwardAmount;
  18.  
  19.  
  20.  
  21.         void Start()
  22.         {
  23.             m_Animator = GetComponent<Animator>();
  24.             m_Rigidbody = GetComponent<Rigidbody>();
  25.  
  26.  
  27.             m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
  28.             m_OrigGroundCheckDistance = m_GroundCheckDistance;
  29.         }
  30.  
  31.  
  32.         public void Move(Vector3 move, bool crouch, bool jump)
  33.         {
  34.  
  35.             // convert the world relative moveInput vector into a local-relative
  36.             // turn amount and forward amount required to head in the desired
  37.             // direction.
  38.             if (move.magnitude > 1f) move.Normalize();
  39.             move = transform.InverseTransformDirection(move);
  40.             move = Vector3.ProjectOnPlane(move, m_GroundNormal);
  41.             m_TurnAmount = Mathf.Atan2(move.x, move.z);
  42.             m_ForwardAmount = move.z;
  43.          
  44.             ApplyExtraTurnRotation();
  45.  
  46.  
  47.             // send input and other state parameters to the animator
  48.             UpdateAnimator(move);
  49.         }
  50.         void UpdateAnimator(Vector3 move)
  51.         {
  52.             // update the animator parameters
  53.             m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
  54.             m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
  55.  
  56.         }
  57.         void ApplyExtraTurnRotation()
  58.         {
  59.             // help the character turn faster (this is in addition to root rotation in the animation)
  60.             float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
  61.             transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
  62.         }
  63.  
  64.  
  65.         public void OnAnimatorMove()
  66.         {
  67.             // we implement this function to override the default root motion.
  68.             // this allows us to modify the positional speed before it's applied.
  69.             if (m_IsGrounded && Time.deltaTime > 0)
  70.             {
  71.                 Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
  72.  
  73.                 // we preserve the existing y part of the current velocity.
  74.                 v.y = m_Rigidbody.velocity.y;
  75.                 m_Rigidbody.velocity = v;
  76.             }
  77.         }
  78.  
  79.  
  80.        
  81.     }
  82. }
Add Comment
Please, Sign In to add comment