Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 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. private GameObject soundPlayer;
  21.  
  22. public bool isGrounded = true;
  23. public bool notFloating = true;
  24. public int yVelocity;
  25. public int xVelocity;
  26. private bool groundCheck = true;
  27. private bool jumpOk;
  28. public bool invincibilityTimer = false;
  29. private bool running = false;
  30. private bool isDead = false;
  31. public bool startDying = false;
  32.  
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. body = GetComponent<Rigidbody2D>();
  37. spriteRenderer = GetComponent<SpriteRenderer>();
  38. headLight = GetComponentInChildren<UnityEngine.Experimental.Rendering.Universal.Light2D>();
  39. player = GetComponent<PlayerHealth>();
  40. soundPlayer = gameObject;
  41. }
  42.  
  43. // Update is called once per frame
  44. void Update()
  45. {
  46. if (Input.GetKeyDown(KeyCode.Space))
  47. {
  48. jumpOk = true;
  49. }
  50. if (Time.timeScale == 0)
  51. {
  52. jumpOk = false;
  53. }
  54. // tsekkaa vielä ehkä parempi?
  55. if (animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Run") && !running)
  56. {
  57. soundPlayer.SendMessage("PlayFootsteps");
  58. running = true;
  59. }
  60. else if(!animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Run") && running)
  61. {
  62. soundPlayer.SendMessage("StopFootsteps");
  63. running = false;
  64. }
  65. if(player.currentHealth == 0)
  66. {
  67. isDead = true;
  68. if (animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Die"))
  69. {
  70. startDying = true;
  71. }
  72. }
  73. }
  74. void OnCollisionStay2D(Collision2D collider)
  75. {
  76. if (collider.gameObject.CompareTag("Ground") || collider.gameObject.CompareTag("Elevator"))
  77. {
  78. int i = 0;
  79. foreach (ContactPoint2D contact in collider.contacts)
  80. {
  81. if (0.15f > (body.transform.position.y - contact.point.y) && (body.transform.position.y - contact.point.y) > 0.147f)
  82. {
  83. isGrounded = true;
  84. }
  85. i++;
  86.  
  87. }
  88. }
  89. }
  90. private void OnTriggerEnter2D(Collider2D other)
  91. {
  92. if (other.gameObject.CompareTag("Hazard") && !invincibilityTimer)
  93. {
  94. soundPlayer.SendMessage("PlayOj");
  95. player.ChangeHealth(-40);
  96. invincibilityTimer = true;
  97. Invoke("ResetInvulnerability", 2);
  98. }
  99.  
  100. }
  101. private void FixedUpdate()
  102. {
  103. if (Time.timeScale > 0)
  104. Move();
  105. }
  106.  
  107. private void Move()
  108. {
  109. bool isJumping = false;
  110. float velocity = Input.GetAxis("Horizontal") * speed;
  111. if (jumpOk)
  112. {
  113. isJumping = Input.GetKey(KeyCode.Space);
  114. }
  115. xVelocity = Mathf.RoundToInt(body.velocity.x);
  116.  
  117. if (!isGrounded)
  118. {
  119. velocity *= airDrag;
  120. }
  121.  
  122. // Horizontal Movement
  123. if(!isDead)
  124. body.velocity = Vector2.SmoothDamp(body.velocity, new Vector2(velocity, body.velocity.y), ref currentVelocity, 0.02f);
  125. // Initiate Jump
  126. yVelocity = Mathf.RoundToInt(body.velocity.y);
  127. if (yVelocity < 0 || yVelocity == 0 && xVelocity != 0)
  128. {
  129. notFloating = true;
  130. }
  131. // Initiate Jump
  132. if (isGrounded && yVelocity == 0 && notFloating == true)
  133. {
  134. groundCheck = true;
  135. if (isJumping && !isDead)
  136. {
  137. body.AddForce(new Vector2(0, jumpForce));
  138. soundPlayer.SendMessage("PlayUmmph");
  139. isGrounded = false;
  140. }
  141. }
  142. else
  143. {
  144. groundCheck = false;
  145. }
  146.  
  147. if (yVelocity > 0)
  148. {
  149. notFloating = false;
  150. }
  151.  
  152. // Cancel Jump
  153. if (!isJumping && body.velocity.y > 0.01f)
  154. {
  155. body.velocity = new Vector2(body.velocity.x, body.velocity.y * 0.95f);
  156. }
  157.  
  158. if (velocity < 0 && spriteRenderer.flipX == false && !isDead)
  159. {
  160. spriteRenderer.flipX = true;
  161. headLight.transform.rotation = Quaternion.Inverse(headLight.transform.rotation);
  162. }
  163. else if (velocity > 0 && spriteRenderer.flipX == true && !isDead)
  164. {
  165. spriteRenderer.flipX = false;
  166. headLight.transform.rotation = Quaternion.Inverse(headLight.transform.rotation);
  167. }
  168.  
  169. animator.SetFloat("VelocityY", Mathf.RoundToInt(body.velocity.y));
  170.  
  171. animator.SetFloat("VelocityX", Mathf.Abs(body.velocity.x));
  172. animator.SetBool("IsGrounded", groundCheck);
  173. animator.SetBool("gameIsOver", isDead);
  174. }
  175. void ResetInvulnerability()
  176. {
  177. invincibilityTimer = false;
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement