Advertisement
jamieTheCoder

PlayerMovement

Sep 2nd, 2022
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using MoreMountains.Feedbacks;
  4. using UnityEngine;
  5.  
  6. public class PlayerMovement : MonoBehaviour {
  7.  
  8.     private Rigidbody2D rb;
  9.     public CharacterController2D controller;
  10.     public float runSpeed = 0f;
  11.     [Header ("GlideStuff")]
  12.     public KeyCode GlideKey; // key to glide
  13.     [SerializeField] private float GlideGravity; // gravity change on glide key press
  14.     float horizontalMove = 0f;
  15.     float gravity;
  16.     bool jump = false;
  17.     bool crouch = false;
  18.     [SerializeField] bool Land = false;
  19.     bool isGrounded;
  20.     bool dustStarted;
  21.     [SerializeField] private int NumberOfJumps;
  22.     private int MaxJumps;
  23.     [Header ("dustSettings")]
  24.     [SerializeField][Range (0, 1)] private float dustStartVelocity; //window at which the dust starts
  25.     [SerializeField][Range (0, 1)] private float dustEndVelocity; // window at which the dust ends
  26.     [Header ("MMFEEDBACKS")] // MMFEEDBACKS JUST MAKES EDITING STUFF AND MAKING ARTISTIC DECISIONS LATER ON FOR THE ARTISTS
  27.     public MMFeedbacks crouchFeedback;
  28.     public MMFeedbacks landFeedback;
  29.     public MMFeedbacks jumpFeedback;
  30.     public MMFeedbacks dustStartFeedback;
  31.     public MMFeedbacks dustPloofFeedback;
  32.  
  33.     private void Start () {
  34.         rb = GetComponent<Rigidbody2D> ();
  35.         gravity = rb.gravityScale;
  36.     }
  37.     // Update is called once per frame
  38.     void Update () {
  39.         //set isgrounded to isgrounded
  40.         isGrounded = controller.m_Grounded;
  41.         //set move variable to button
  42.         horizontalMove = Input.GetAxisRaw ("Horizontal") * runSpeed;
  43.         SetJumpVariables ();
  44.         DustCode ();
  45.         MovementStates ();
  46.     }
  47.  
  48.     void FixedUpdate () {
  49.         //move our character
  50.         controller.Move (horizontalMove * Time.fixedDeltaTime, crouch, jump);
  51.         jump = false;
  52.     }
  53.     void DustCode () {
  54.         //dust code__________________________________________________________________________________________________________________
  55.         if (isGrounded) {
  56.             if (!Land) {
  57.                 landFeedback.PlayFeedbacks ();
  58.                 Land = true;
  59.             }
  60.             if (Mathf.Abs (rb.velocity.x) >= dustStartVelocity) {
  61.                 if (!dustStarted) {
  62.                     dustStartFeedback.PlayFeedbacks ();
  63.                     dustStarted = true;
  64.                 }
  65.                 if (horizontalMove == 0) {
  66.                     bool stop = false;
  67.                     if (stop == false) {
  68.                         dustPloofFeedback.PlayFeedbacks ();
  69.                         stop = true;
  70.                     }
  71.                 }
  72.             } else if (Mathf.Abs (rb.velocity.x) <= dustEndVelocity) {
  73.                 if (horizontalMove == 0) {
  74.                     if (dustStarted) {
  75.                         dustStartFeedback.StopFeedbacks ();
  76.                         dustStarted = false;
  77.                     }
  78.                 }
  79.             } else { }
  80.         } else if (!isGrounded) {
  81.             Glide ();
  82.             dustStartFeedback.StopFeedbacks ();
  83.             dustStarted = false;
  84.         }
  85.         //end of dust code________________________________________________________________________________________
  86.     }
  87.  
  88.     void Glide () {
  89.         if (Input.GetKeyDown (GlideKey)) {
  90.             rb.gravityScale = GlideGravity;
  91.         } else if (Input.GetKeyUp (GlideKey)) {
  92.             rb.gravityScale = gravity;
  93.         }
  94.     }
  95.  
  96.     void MovementStates () {
  97.         if (Input.GetButtonDown ("Jump")) {
  98.             if (NumberOfJumps != MaxJumps) {
  99.                 jumpFeedback.PlayFeedbacks ();
  100.                 jump = true;
  101.                 Land = false;
  102.             } else { }
  103.         }
  104.         if (Input.GetButtonDown ("Crouch")) {
  105.             crouch = true;
  106.             crouchFeedback.PlayFeedbacks ();
  107.         } else if (Input.GetButtonUp ("Crouch")) {
  108.             crouch = false;
  109.             crouchFeedback.PlayFeedbacksInReverse ();
  110.         }
  111.     }
  112.     void SetJumpVariables () {
  113.         NumberOfJumps = controller.NumberOfJumps;
  114.         MaxJumps = controller.MaxJumps;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement