Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7. public float speed = 100.0f;
  8. public float jumpForce = 350.0f;
  9. public float airDrag = 0.8f;
  10.  
  11. private Rigidbody2D body;
  12. private SpriteRenderer spriteRenderer;
  13. public Animator animator;
  14. private UnityEngine.Experimental.Rendering.Universal.Light2D headLight;
  15. PlayerHealth player;
  16.  
  17. private Vector2 currentVelocity;
  18. private float previousPositionY;
  19. public Grid background;
  20.  
  21. public bool isGrounded = true;
  22. public bool notFloating = true;
  23. public int yVelocity;
  24. public int xVelocity;
  25. private bool groundCheck = true;
  26. private bool jumpOk;
  27. public bool invincibilityTimer = false;
  28.  
  29. // Start is called before the first frame update
  30. void Start()
  31. {
  32. body = GetComponent<Rigidbody2D>();
  33. spriteRenderer = GetComponent<SpriteRenderer>();
  34. headLight = GetComponentInChildren<UnityEngine.Experimental.Rendering.Universal.Light2D>();
  35. player = GetComponent<PlayerHealth>();
  36.  
  37. }
  38.  
  39. // Update is called once per frame
  40. void Update()
  41. {
  42. if (Input.GetKeyDown(KeyCode.Space))
  43. {
  44. jumpOk = true;
  45. }
  46. if (Time.timeScale == 0)
  47. {
  48. jumpOk = false;
  49. }
  50. }
  51. void OnCollisionStay2D(Collision2D collider)
  52. {
  53. if (collider.gameObject.CompareTag("Ground"))
  54. {
  55. int i = 0;
  56. foreach (ContactPoint2D contact in collider.contacts)
  57. {
  58. if (0.15f > (body.transform.position.y - contact.point.y) && (body.transform.position.y - contact.point.y) > 0.147f)
  59. {
  60. isGrounded = true;
  61. }
  62. i++;
  63.  
  64. }
  65. }
  66. }
  67. private void OnTriggerEnter2D(Collider2D other)
  68. {
  69. if (other.gameObject.CompareTag("Hazard") && !invincibilityTimer)
  70. {
  71. Debug.Log("dis hurts");
  72. player.ChangeHealth(-40);
  73. invincibilityTimer = true;
  74. Invoke("ResetInvulnerability", 2);
  75. }
  76.  
  77. }
  78. private void FixedUpdate()
  79. {
  80. if (Time.timeScale > 0)
  81. Move();
  82. }
  83.  
  84. private void Move()
  85. {
  86. bool isJumping = false;
  87. float velocity = Input.GetAxis("Horizontal") * speed;
  88. if (jumpOk)
  89. {
  90. isJumping = Input.GetKey(KeyCode.Space);
  91. }
  92. xVelocity = Mathf.RoundToInt(body.velocity.x);
  93.  
  94. if (!isGrounded)
  95. {
  96. velocity *= airDrag;
  97. }
  98.  
  99. // Horizontal Movement
  100. body.velocity = Vector2.SmoothDamp(body.velocity, new Vector2(velocity, body.velocity.y), ref currentVelocity, 0.02f);
  101. // Initiate Jump
  102. yVelocity = Mathf.RoundToInt(body.velocity.y);
  103. if (yVelocity < 0 || yVelocity == 0 && xVelocity != 0)
  104. {
  105. notFloating = true;
  106. }
  107. // Initiate Jump
  108. if (isGrounded && yVelocity == 0 && notFloating == true)
  109. {
  110. groundCheck = true;
  111. if (isJumping)
  112. {
  113. body.AddForce(new Vector2(0, jumpForce));
  114. isGrounded = false;
  115. }
  116. }
  117. else
  118. {
  119. groundCheck = false;
  120. }
  121.  
  122. if (yVelocity > 0)
  123. {
  124. notFloating = false;
  125. }
  126.  
  127. // Cancel Jump
  128. if (!isJumping && body.velocity.y > 0.01f)
  129. {
  130. body.velocity = new Vector2(body.velocity.x, body.velocity.y * 0.95f);
  131. }
  132.  
  133. if (velocity < 0 && spriteRenderer.flipX == false)
  134. {
  135. spriteRenderer.flipX = true;
  136. headLight.transform.rotation = Quaternion.Inverse(headLight.transform.rotation);
  137. }
  138. else if (velocity > 0 && spriteRenderer.flipX == true)
  139. {
  140. spriteRenderer.flipX = false;
  141. headLight.transform.rotation = Quaternion.Inverse(headLight.transform.rotation);
  142. }
  143.  
  144.  
  145. animator.SetFloat("VelocityY", Mathf.RoundToInt(body.velocity.y));
  146.  
  147. animator.SetFloat("VelocityX", Mathf.Abs(body.velocity.x));
  148. animator.SetBool("IsGrounded", groundCheck);
  149. }
  150. void ResetInvulnerability()
  151. {
  152. invincibilityTimer = false;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement