Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Controller3 : MonoBehaviour
  5. {
  6. public Animator animator;
  7. bool death = false;
  8. public class GroundState
  9. {
  10. private GameObject player;
  11. private float width;
  12. private float height;
  13. private float length;
  14.  
  15. //GroundState constructor. Sets offsets for raycasting.
  16. public GroundState(GameObject playerRef)
  17. {
  18. player = playerRef;
  19. width = player.GetComponent<Collider2D>().bounds.extents.x + 0.1f;
  20. height = player.GetComponent<Collider2D>().bounds.extents.y + 0.2f;
  21. length = 0.05f;
  22. }
  23.  
  24. //Returns whether or not player is touching wall.
  25. public bool isWall()
  26. {
  27. bool left = Physics2D.Raycast(new Vector2(player.transform.position.x - (width), player.transform.position.y), -Vector2.right, length);
  28. bool right = Physics2D.Raycast(new Vector2(player.transform.position.x + (width), player.transform.position.y), Vector2.right, length);
  29.  
  30. if (left || right)
  31. return true;
  32. else
  33. return false;
  34. }
  35.  
  36. //Returns whether or not player is touching ground.
  37. public bool isGround()
  38. {
  39. bool bottom1 = Physics2D.Raycast(new Vector2(player.transform.position.x, player.transform.position.y - height), -Vector2.up, length);
  40. bool bottom2 = Physics2D.Raycast(new Vector2(player.transform.position.x + (width - 0.1f), player.transform.position.y - height), -Vector2.up, length);
  41. bool bottom3 = Physics2D.Raycast(new Vector2(player.transform.position.x - (width - 0.1f), player.transform.position.y - height), -Vector2.up, length);
  42.  
  43. if (bottom1 || bottom2 || bottom3)
  44. return true;
  45. else
  46. return false;
  47. }
  48.  
  49. //Returns whether or not player is touching wall or ground.
  50. public bool isTouching()
  51. {
  52. if (isGround() || isWall())
  53. return true;
  54. else
  55. return false;
  56. }
  57.  
  58. //Returns direction of wall.
  59. public int wallDirection()
  60. {
  61. bool left1 = Physics2D.Raycast(new Vector2(player.transform.position.x - width, player.transform.position.y), -Vector2.right, length);
  62. bool right1 = Physics2D.Raycast(new Vector2(player.transform.position.x + width, player.transform.position.y), Vector2.right, length);
  63.  
  64. if (left1)
  65. return -1;
  66. else if (right1)
  67. return 1;
  68. else
  69. return 0;
  70. }
  71. }
  72. IEnumerator Delay()
  73. {
  74. powerJump = false;
  75. GetComponent<Animator>().Play("Player_Death", 0, 0f);
  76. death = true;
  77. gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
  78. gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY;
  79. GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
  80. GetComponent<Rigidbody2D>().gravityScale = 0.0f;
  81. yield return new WaitForSeconds(1);
  82. GameManager.instance.ResetLevel();
  83. }
  84. void OnTriggerEnter2D(Collider2D other)
  85. {
  86. //When touching the trap.
  87. if (other.CompareTag("Trap") && !death)
  88. {
  89. StartCoroutine(Delay());
  90. }
  91. //When touching the finish.
  92. else if (other.CompareTag("Finish"))
  93. {
  94. //Go to next level.
  95. GameManager.instance.IncreaseLevel();
  96. }
  97. }
  98. public float speed = 14f;
  99. public float accel = 6f;
  100. public float airAccel = 3f;
  101. public float jump = 14f;
  102. public float powerJumpStopBrakeMultiplier = 0.5f;
  103.  
  104. private GroundState groundState;
  105. private bool powerJump;
  106.  
  107. void Start()
  108. {
  109. //Create an object to check if player is grounded or touching wall
  110. groundState = new GroundState(transform.gameObject);
  111. }
  112.  
  113. private Vector2 input;
  114.  
  115.  
  116.  
  117. void Update()
  118. {
  119. if (death == true)
  120. {
  121. return;
  122. }
  123.  
  124. //Handle input
  125. if (Input.GetKey(KeyCode.LeftArrow))
  126. input.x = -1;
  127. else if (Input.GetKey(KeyCode.RightArrow))
  128. input.x = 1;
  129. else
  130. input.x = 0;
  131.  
  132. if (Input.GetButtonDown("Jump"))
  133. input.y = 1;
  134.  
  135. //Stopped powerjumping
  136. if (powerJump && Input.GetButtonUp("Jump"))
  137. {
  138. powerJump = false;
  139. GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, GetComponent<Rigidbody2D>().velocity.y * powerJumpStopBrakeMultiplier);
  140. }
  141.  
  142. //Reverse player if going different direction
  143. transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, (input.x == 0) ? transform.localEulerAngles.y : (input.x + 1) * 90, transform.localEulerAngles.z);
  144. //Check if run into obstacle.
  145.  
  146.  
  147. }
  148.  
  149. void FixedUpdate()
  150. {
  151. if (death == true)
  152. {
  153. return;
  154. }
  155.  
  156. if (powerJump && (groundState.isTouching() || GetComponent<Rigidbody2D>().velocity.y < 0))
  157. {
  158. powerJump = false;
  159. }
  160.  
  161. if (input.y == 1 && groundState.isTouching())
  162. powerJump = true;
  163.  
  164. GetComponent<Rigidbody2D>().AddForce(new Vector2(((input.x * speed) - GetComponent<Rigidbody2D>().velocity.x) * (groundState.isGround() ? accel : airAccel), 0)); //Move player.
  165. //Stop player if input.x is 0 (and grounded) and jump if input.y is 1
  166. GetComponent<Rigidbody2D>().velocity = new Vector2((input.x == 0 && groundState.isGround()) ? 0 : GetComponent<Rigidbody2D>().velocity.x, (input.y == 1 && groundState.isTouching()) ? jump : GetComponent<Rigidbody2D>().velocity.y);
  167.  
  168.  
  169. if (groundState.isWall() && !groundState.isGround() && input.y == 1)
  170. GetComponent<Rigidbody2D>().velocity = new Vector2(-groundState.wallDirection() * speed * 0.75f, GetComponent<Rigidbody2D>().velocity.y); //Add force negative to wall direction (with speed reduction)
  171.  
  172. input.y = 0;
  173.  
  174. animator.SetFloat("horizontal", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
  175. animator.SetFloat("vertical", GetComponent<Rigidbody2D>().velocity.y);
  176. animator.SetBool("grounded", groundState.isGround());
  177. animator.SetBool("walled", groundState.isWall());
  178. //animator.SetBool("dead", death);
  179.  
  180. }
  181. //Enables being carried by moving objects tagged as "platform"
  182. void OnCollisionEnter2D(Collision2D col)
  183. {
  184. if (col.gameObject.CompareTag("Platform"))
  185. this.transform.parent = col.transform;
  186. }
  187. void OnCollisionExit2D(Collision2D col)
  188. {
  189. if (col.gameObject.CompareTag("Platform"))
  190. this.transform.parent = null;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement