Advertisement
TeletubProd

player_mvt

Jun 27th, 2022 (edited)
642
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class player_mvt : MonoBehaviour
  6. {
  7.     Rigidbody2D rb;
  8.  
  9.     [Header("Mouvement")]
  10.     public float maxSpeed;
  11.     public float slipperiness;
  12.     [Range(0,1)]
  13.     public float mvtDamp, turnDamp, stopDamp;
  14.  
  15.     [Space(15)]
  16.     public float jumpTime;
  17.     float _jumpTime;
  18.     [Range(0,1)]
  19.     public float jumpCutMultiplier;
  20.     [Range(1,2)]
  21.     public float fallMultiplier;
  22.     public float maxGravity, baseGravity;
  23.     [Range(0,1)]
  24.     public float flyGravMult;
  25.     public bool flying = false;
  26.     Coroutine flycor;
  27.     [Range(0,1)]
  28.     public float airMvtDamp, airTurnDamp, airStopDamp;
  29.     public bool noFlip;
  30.  
  31.     [Header("Collisions")]
  32.     public Bounds groundCheck;
  33.     public LayerMask Ground;
  34.     public float mx, my;
  35.     public bool isJump;
  36.     float rota = 0;
  37.     public Bounds platCheck;
  38.     public Bounds platEnd;
  39.     Coroutine dropCor;
  40.     [Space(15)]
  41.     public bool can_move = true;
  42.     float xStore = 0, yStore = 0;
  43.  
  44.     void Start()
  45.     {
  46.         rb = GetComponent<Rigidbody2D>();
  47.         _jumpTime = jumpTime;
  48.     }
  49.  
  50.     void FixedUpdate()
  51.     {
  52.         Flip();
  53.         airMove();
  54.         // obsolete
  55.         //Xmove();
  56.         //Fall();
  57.     }
  58.  
  59.     void airMove()
  60.     {
  61.         if(!can_move) return;
  62.  
  63.         float xspeed = mx * player_stats.MVT();
  64.         float yspeed = my * player_stats.MVT();
  65.  
  66.         xStore = velocCalc(ref xStore, xspeed, mx);
  67.         yStore = velocCalc(ref yStore, yspeed, my);
  68.  
  69.         rb.velocity = (can_move)? new Vector2(xStore, yStore) : rb.velocity;
  70.     }
  71.  
  72.     float velocCalc(ref float velstore, float veloc, float dir)
  73.     {
  74.         velstore += veloc + dir;
  75.  
  76.         float stop = Grounded()? stopDamp : airStopDamp;
  77.         float turn = Grounded()? turnDamp : airTurnDamp;
  78.         float damp = Grounded()? mvtDamp : airMvtDamp;
  79.  
  80.         if (Mathf.Abs(velstore) > player_stats.MVT())
  81.         {
  82.             velstore =  player_stats.MVT() * dir;
  83.         }
  84.  
  85.         if (Mathf.Abs(dir) < 0.01f)
  86.         {
  87.             velstore *= Mathf.Pow(1f - stop, Time.fixedDeltaTime * slipperiness);
  88.         }
  89.         else if (Mathf.Sign(dir) != Mathf.Sign(velstore))
  90.         {
  91.             velstore *= Mathf.Pow(1f - turn, Time.fixedDeltaTime * slipperiness);
  92.         }
  93.         else
  94.         {
  95.             velstore *= Mathf.Pow(1f - damp, Time.fixedDeltaTime * slipperiness);
  96.         }
  97.  
  98.         return velstore;
  99.     }
  100.  
  101.     void Flip()
  102.     {
  103.         if(!can_move) return;
  104.         if(noFlip) return;
  105.  
  106.         if(mx > 0.1f) rota = 0;
  107.         if(mx < -0.1f) rota = 180;
  108.  
  109.         transform.rotation = Quaternion.Euler(0,rota,0);
  110.     }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.     public bool Grounded()
  118.     {
  119.         Collider2D ray = Physics2D.OverlapBox(transform.position + groundCheck.center, groundCheck.extents,0, Ground);
  120.  
  121.         if(ray != null)
  122.         {
  123.             return true;
  124.         }
  125.         else
  126.         {
  127.             return false;
  128.         }
  129.     }
  130.  
  131.     void OnDrawGizmos()
  132.     {
  133.         Gizmos.color = Color.blue;
  134.         Gizmos.DrawWireCube(transform.position + groundCheck.center, groundCheck.extents);
  135.         Gizmos.color = Color.magenta;
  136.         Gizmos.DrawWireCube(transform.position + platCheck.center, platCheck.extents);
  137.         Gizmos.color = Color.yellow;
  138.         Gizmos.DrawWireCube(transform.position + platEnd.center, platEnd.extents);
  139.     }
  140. }
  141.  
Advertisement
RAW Paste Data Copied
Advertisement