Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----------------------------------------------------------
- // o PlayerMovement.cs
- //
- // ----------------------------------------------------------
- using UnityEngine;
- // ----------------------------------------------------------
- // • NameSpace
- // ----------------------------------------------------------
- namespace LeTutalQuiSentDesPieds {
- // ----------------------------------------------------------
- // • PlayerMovement
- // ----------------------------------------------------------
- public class PlayerMovement : MonoBehaviour {
- public float TurnSpeed = 20f;
- private Animator _animator;
- private Rigidbody _rigidBody;
- private Vector3 _movement = Vector3.zero;
- private Quaternion _rotation = Quaternion.identity;
- // ----------------------------------------------------------
- // • Start
- // ----------------------------------------------------------
- void Start() {
- this._animator = GetComponent<Animator>();
- this._rigidBody = GetComponent<Rigidbody>();
- }
- // ----------------------------------------------------------
- // • FixedUpdate
- // ----------------------------------------------------------
- void FixedUpdate() {
- // Inputs
- this._movement.x = Input.GetAxis("Horizontal");
- this._movement.z = Input.GetAxis("Vertical");
- this._movement.Normalize();
- // Walking Animation
- bool isWalking = Mathf.Abs(this._movement.x) > .0001f || Mathf.Abs(this._movement.z) > .0001f;
- this._animator.SetBool("IsWalking", isWalking);
- // Look Rotation
- Vector3 look = Vector3.RotateTowards(this.transform.forward, this._movement, this.TurnSpeed * Time.fixedDeltaTime, 0f);
- this._rotation = Quaternion.LookRotation(look);
- }
- // ----------------------------------------------------------
- // • OnAnimatorMove
- // ----------------------------------------------------------
- void OnAnimatorMove() {
- this._rigidBody.MovePosition(this._rigidBody.position + this._movement * this._animator.deltaPosition.magnitude);
- this._rigidBody.MoveRotation(this._rotation);
- }
- // ---
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement