Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7. public class PlayerController : MonoBehaviour
  8. {
  9.  
  10. //User Interface.
  11. //DO NOT REMOVE
  12. public Text txtHealth;
  13. public Text txtLives;
  14. public Text txtCollectables;
  15.  
  16. public float speed = 10;
  17. public float jumpForce = 10;
  18. public int lives = 3;
  19. public int health = 100;
  20. public int collectables= 0;
  21.  
  22. bool isOnGround = false;
  23. Vector2 force;
  24. Rigidbody2D body;
  25.  
  26. private Vector3 startPosition;
  27.  
  28. void Start()
  29. {
  30. force.y = jumpForce;
  31. body = GetComponent<Rigidbody2D>();
  32. startPosition = transform.position;
  33. }
  34.  
  35. void Update()
  36. {
  37. UpdateUI();
  38.  
  39.  
  40. //if the spacebar is released
  41. if(Input.GetKeyUp(KeyCode.Space))
  42. {
  43. //if the player is on the ground
  44. if (isOnGround)
  45. {
  46. //add force to the players rigid body
  47. body.AddForce(force, ForceMode2D.Impulse);
  48. isOnGround = false;
  49. }
  50.  
  51. }
  52.  
  53. float x = Input.GetAxis("Horizontal");
  54. //set the players X velocity to x * speed
  55. //set the players Y velocity to be the current Y velocity
  56. body.velocity = new Vector2(x * speed, body.velocity.y);
  57.  
  58. }
  59.  
  60. //DO NOT REMOVE
  61. void UpdateUI()
  62. {
  63.  
  64. txtHealth.text = "Health: " + health;
  65. txtLives.text = "Lives: " + lives;
  66. txtCollectables.text = "Collectables: " + collectables;
  67. if (health <= 0)
  68. {
  69. lives -= 1;
  70. health = 100;
  71.  
  72. }
  73. else if (lives <= 0)
  74. {
  75. int sceneIndex = SceneManager.GetActiveScene().buildIndex;
  76. SceneManager.LoadScene(sceneIndex);
  77.  
  78. }
  79. }
  80.  
  81. private void OnCollisionEnter2D(Collision2D collision)
  82. {
  83. string tag = collision.gameObject.tag;
  84.  
  85. if (tag == "ground")
  86. {
  87. isOnGround = true;
  88. }
  89. else if (collision.gameObject.tag == "collectables")
  90. {
  91. collectables++;
  92. Destroy(collision.gameObject);
  93. }
  94. else if (collision.gameObject.tag == "kill")
  95. {
  96. health -= 50;
  97. transform.position = startPosition;
  98. }
  99. else if (collision.gameObject.tag == "door")
  100. {
  101. GetComponent<DoorController>();
  102. if (collectables >= 1)
  103. {
  104. Destroy(collision.gameObject);
  105. collectables--;
  106. }
  107. }
  108. else if (lives <= 0)
  109. {
  110. int sceneIndex = SceneManager.GetActiveScene().buildIndex;
  111. SceneManager.LoadScene(sceneIndex);
  112.  
  113. }
  114.  
  115.  
  116. }
  117.  
  118. private void OnTriggerEnter2D(Collider2D collision)
  119. {
  120. if (collision.gameObject.tag == "collectable")
  121. {
  122. collectables++;
  123. Destroy(collision.gameObject);
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement