Advertisement
TeletubProd

Untitled

Jun 11th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  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 jumpForce;
  17. public float jumpTime;
  18. float _jumpTime;
  19. [Range(0,1)]
  20. public float jumpCutMultiplier;
  21. [Range(1,2)]
  22. public float fallMultiplier;
  23. public float maxGravity, baseGravity;
  24. [Range(0,1)]
  25. public float airMvtDamp, airTurnDamp, airStopDamp;
  26. public bool noFlip;
  27.  
  28. [Header("Collisions")]
  29. public Bounds groundCheck;
  30. public LayerMask Ground;
  31. public float mx, my;
  32. public bool isJump;
  33. float rota = 0;
  34. public Bounds platCheck;
  35. public Bounds platEnd;
  36. Coroutine dropCor;
  37. [Space(15)]
  38. public bool can_move = true;
  39. int accel = 0;
  40.  
  41. void Start()
  42. {
  43. rb = GetComponent<Rigidbody2D>();
  44. _jumpTime = jumpTime;
  45. }
  46.  
  47. void FixedUpdate()
  48. {
  49. Xmove();
  50. Flip();
  51. Fall();
  52. }
  53.  
  54.  
  55. void Flip()
  56. {
  57. if(!can_move) return;
  58. if(noFlip) return;
  59.  
  60. if(mx > 0.1f) rota = 0;
  61. if(mx < -0.1f) rota = 180;
  62.  
  63. transform.rotation = Quaternion.Euler(0,rota,0);
  64. }
  65.  
  66. void Fall()
  67. {
  68. if(rb.velocity.y <= 0 && !Grounded())
  69. {
  70. rb.gravityScale = Gravity(rb.gravityScale);
  71. }
  72. else
  73. {
  74. rb.gravityScale = baseGravity;
  75. }
  76. }
  77.  
  78. float Gravity(float gravity)
  79. {
  80. if(gravity < baseGravity) return baseGravity;
  81. if(gravity > maxGravity) return maxGravity;
  82.  
  83. return gravity * fallMultiplier;
  84. }
  85.  
  86. void Update()
  87. {
  88. print(rb.velocity.x);
  89. }
  90.  
  91. void Xmove()
  92. {
  93. if(!can_move) return;
  94.  
  95. float veloc = rb.velocity.x;
  96. veloc += (float)mx;
  97.  
  98. rb.velocity = new Vector2(veloc, rb.velocity.y);
  99.  
  100. }
  101.  
  102. float MoveDamp(float stop, float turn, float damp)
  103. {
  104. float hVeloc = rb.velocity.x;
  105. hVeloc += mx;
  106.  
  107. // if (Mathf.Abs(hVeloc) > timst.mvtCalc(maxSpeed))
  108. // {
  109. // hVeloc = timst.mvtCalc(maxSpeed) * mx;
  110. // }
  111.  
  112. // if (Mathf.Abs(mx) < 0.01f)
  113. // {
  114. // hVeloc *= Mathf.Pow(1f - stop, Time.fixedDeltaTime * slipperiness);
  115. // }
  116. // else if (Mathf.Sign(mx) != Mathf.Sign(hVeloc))
  117. // {
  118. // hVeloc *= Mathf.Pow(1f - turn, Time.fixedDeltaTime * slipperiness);
  119. // }
  120. // else
  121. // {
  122. // hVeloc *= Mathf.Pow(1f - damp, Time.fixedDeltaTime * slipperiness);
  123. // }
  124.  
  125. return hVeloc;
  126. }
  127.  
  128. public void JumpStart()
  129. {
  130. isJump = true;
  131. _jumpTime = jumpTime;
  132. rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
  133. }
  134.  
  135. public void Jump()
  136. {
  137. if(_jumpTime <= 0) isJump = false;
  138. if(!isJump) return;
  139. if(!can_move) return;
  140.  
  141. rb.velocity = new Vector2(rb.velocity.x, jumpForce);
  142. _jumpTime -= Time.deltaTime;
  143. }
  144.  
  145. public void StopJump()
  146. {
  147. if(!can_move) return;
  148. isJump = false;
  149.  
  150. if (rb.velocity.y > 0 && !Grounded())
  151. {
  152. rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * jumpCutMultiplier);
  153. }
  154. }
  155.  
  156. public void Drop()
  157. {
  158. if(!can_move) return;
  159. LayerMask platmask = LayerMask.GetMask("Platform");
  160. Collider2D ray = Physics2D.OverlapBox(transform.position + platCheck.center, platCheck.extents,0, platmask);
  161.  
  162. if(ray != null)
  163. {
  164. Physics2D.IgnoreLayerCollision(3, 11, true);
  165.  
  166. if(dropCor != null) StopCoroutine(dropCor);
  167. dropCor = StartCoroutine(DropCor());
  168. }
  169. }
  170.  
  171. IEnumerator DropCor()
  172. {
  173. yield return new WaitUntil(() => !PlatEnd());
  174.  
  175. Physics2D.IgnoreLayerCollision(3, 11, false);
  176. dropCor = null;
  177. }
  178.  
  179. public bool PlatEnd()
  180. {
  181. LayerMask platmask = LayerMask.GetMask("Platform");
  182. Collider2D ray = Physics2D.OverlapBox(transform.position + platEnd.center, platEnd.extents,0, platmask);
  183.  
  184. if(ray != null)
  185. {
  186. return true;
  187. }
  188. else
  189. {
  190. return false;
  191. }
  192. }
  193.  
  194. public bool Grounded()
  195. {
  196. Collider2D ray = Physics2D.OverlapBox(transform.position + groundCheck.center, groundCheck.extents,0, Ground);
  197.  
  198. if(ray != null)
  199. {
  200. return true;
  201. }
  202. else
  203. {
  204. return false;
  205. }
  206. }
  207.  
  208. void OnDrawGizmos()
  209. {
  210. Gizmos.color = Color.blue;
  211. Gizmos.DrawWireCube(transform.position + groundCheck.center, groundCheck.extents);
  212. Gizmos.color = Color.magenta;
  213. Gizmos.DrawWireCube(transform.position + platCheck.center, platCheck.extents);
  214. Gizmos.color = Color.yellow;
  215. Gizmos.DrawWireCube(transform.position + platEnd.center, platEnd.extents);
  216. }
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement