Advertisement
Guest User

Untitled

a guest
May 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. // ----------------------------------------------------------
  2. //  o PlayerMovement.cs
  3. //
  4. // ----------------------------------------------------------
  5. using UnityEngine;
  6.  
  7. // ----------------------------------------------------------
  8. //  • NameSpace
  9. // ----------------------------------------------------------
  10. namespace LeTutalQuiSentDesPieds {
  11.     // ----------------------------------------------------------
  12.     //  • PlayerMovement
  13.     // ----------------------------------------------------------
  14.     public class PlayerMovement : MonoBehaviour {
  15.         public float TurnSpeed = 20f;
  16.         private Animator _animator;
  17.         private Rigidbody _rigidBody;
  18.         private Vector3 _movement = Vector3.zero;
  19.         private Quaternion _rotation = Quaternion.identity;
  20.         // ----------------------------------------------------------
  21.         //  • Start
  22.         // ----------------------------------------------------------
  23.         void Start() {
  24.             this._animator = GetComponent<Animator>();
  25.             this._rigidBody = GetComponent<Rigidbody>();
  26.         }
  27.         // ----------------------------------------------------------
  28.         //  • FixedUpdate
  29.         // ----------------------------------------------------------
  30.         void FixedUpdate() {
  31.             // Inputs
  32.             this._movement.x = Input.GetAxis("Horizontal");
  33.             this._movement.z = Input.GetAxis("Vertical");
  34.             this._movement.Normalize();
  35.             // Walking Animation
  36.             bool isWalking = Mathf.Abs(this._movement.x) > .0001f || Mathf.Abs(this._movement.z) > .0001f;
  37.             this._animator.SetBool("IsWalking", isWalking);
  38.             // Look Rotation
  39.             Vector3 look = Vector3.RotateTowards(this.transform.forward, this._movement, this.TurnSpeed * Time.fixedDeltaTime, 0f);
  40.             this._rotation = Quaternion.LookRotation(look);
  41.         }
  42.         // ----------------------------------------------------------
  43.         //  • OnAnimatorMove
  44.         // ----------------------------------------------------------
  45.         void OnAnimatorMove() {
  46.             this._rigidBody.MovePosition(this._rigidBody.position + this._movement * this._animator.deltaPosition.magnitude);
  47.             this._rigidBody.MoveRotation(this._rotation);
  48.         }
  49.         // ---
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement