Advertisement
Guest User

Untitled

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