Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8. public SCR_Fireball Fireball;
  9.  
  10. //Access the Players Rigidbody
  11. public Rigidbody playerRigidbody;
  12.  
  13. //Hitpoints and Manapoints
  14. public float p_MaxHitpoints = 100;
  15. public float p_CurrentHitpoints;
  16. public Text HealthText;
  17. public float p_MaxManapoints = 100;
  18. public float p_CurrentManapoints;
  19. public Text ManaText;
  20. public bool TakingDamage = false;
  21.  
  22.  
  23. //Set players Jump Heigh
  24. public float jumpForce = 100;
  25.  
  26. //Set the players movement speed
  27. public float movementSpeed = 5;
  28.  
  29. //Check if the player is touching the ground
  30. private bool grounded = true;
  31.  
  32.  
  33. //Is scepter equiped
  34. public bool SceptorEquip = true;
  35. public GameObject Scepter;
  36.  
  37. //Is crossbow Equiped
  38. public bool CrossbowEquip = false;
  39. public GameObject Crossbow;
  40.  
  41. //Damage Over Time Coroutine
  42. private IEnumerator DamageOverTime()
  43. {
  44. while (TakingDamage == true)
  45. {
  46.  
  47. p_CurrentHitpoints -= 5;
  48. yield return new WaitForSeconds(2.0f);
  49. TakingDamage = false;
  50. }
  51.  
  52. }
  53.  
  54. // Use this for initialization
  55. void Start ()
  56. {
  57. //Set hitpoints and Manapoints
  58. p_CurrentHitpoints = p_MaxHitpoints;
  59. p_CurrentManapoints = p_MaxManapoints;
  60.  
  61. //Disable Cursor in-game
  62. Cursor.lockState = CursorLockMode.Locked;
  63. Scepter.gameObject.SetActive(true);
  64. Crossbow.gameObject.SetActive(false);
  65. }
  66.  
  67. // Update is called once per frame
  68. void Update ()
  69. {
  70.  
  71.  
  72. //Enable Cursor on button press
  73. if (Input.GetKeyDown("escape"))
  74. Cursor.lockState = CursorLockMode.None;
  75.  
  76. //Hit points and Mana points
  77.  
  78. HealthText.text = "" + p_CurrentHitpoints;
  79. ManaText.text = "" + p_CurrentManapoints;
  80.  
  81.  
  82. //Movement
  83. float horizontalAxis = Input.GetAxis("Horizontal");
  84. float verticalAxis = Input.GetAxis("Vertical");
  85.  
  86. Vector3 playerRight = transform.right;
  87. // Players local forward axis
  88. Vector3 playerForward = transform.forward;
  89.  
  90. // Setting velocity to the local axis
  91. Vector3 xVelocity = transform.right * horizontalAxis;
  92.  
  93. // Change value for forward and back movement.
  94. Vector3 zVelocity = transform.forward * verticalAxis;
  95. Vector3 currentTotalVelocity = xVelocity + zVelocity;
  96.  
  97. // Normalized vectors reduce lenght to 1 to prevent a faster speed moving diagonally
  98. currentTotalVelocity = currentTotalVelocity.normalized * movementSpeed;
  99. currentTotalVelocity.y = playerRigidbody.velocity.y;
  100. playerRigidbody.velocity = currentTotalVelocity;
  101.  
  102. //Get input for Mouse X and Y
  103. float mouseX = Input.GetAxis("Mouse X");
  104. float mouseY = Input.GetAxis("Mouse Y");
  105.  
  106. // Change Local Euler Angles on key press
  107. Vector3 currentRotation = transform.localEulerAngles;
  108.  
  109. // Rotate from MouseX input
  110. if (Input.GetAxis("Mouse X") < 0)
  111. {
  112. currentRotation.y -= 3;
  113. }
  114.  
  115. if (Input.GetAxis("Mouse X") > 0)
  116. {
  117. currentRotation.y += 3;
  118. }
  119. transform.localEulerAngles = currentRotation;
  120.  
  121. //Mouse Y can rotate all the way around.
  122. /*
  123. if (Input.GetAxis("Mouse Y") < 0)
  124. {
  125. currentRotation.x += 3;
  126. }
  127.  
  128. if (Input.GetAxis("Mouse Y") > 0)
  129. {
  130. currentRotation.x -= 3;
  131. }
  132. transform.localEulerAngles = currentRotation;
  133.  
  134. */
  135. //Jump
  136. if (Input.GetKeyDown("space"))
  137. {
  138. if (grounded)
  139. {
  140. playerRigidbody.AddForce(0, jumpForce, jumpForce);
  141. grounded = false;
  142. }
  143.  
  144. }
  145.  
  146.  
  147. //Weapon Swap for Scepter
  148. if(Input.GetKeyDown("1"))
  149. {
  150. Scepter.gameObject.SetActive(true);
  151. SceptorEquip = true;
  152. Crossbow.gameObject.SetActive(false);
  153. CrossbowEquip = false;
  154. }
  155. //Weapon Swap for Crossbow
  156. if (Input.GetKeyDown("2"))
  157. {
  158. Scepter.gameObject.SetActive(false);
  159. SceptorEquip = false;
  160. Crossbow.gameObject.SetActive(true);
  161. CrossbowEquip = true;
  162. }
  163. //Death
  164. if (p_CurrentHitpoints <= 0)
  165. {
  166. Destroy(gameObject);
  167. }
  168. }
  169.  
  170. private void OnCollisionEnter(Collision collision)
  171. {
  172. if (collision.transform.tag == "Ground")
  173. {
  174. TakingDamage = false;
  175. grounded = true;
  176. StopCoroutine(DamageOverTime());
  177.  
  178. }
  179.  
  180. if (collision.transform.tag == "Enemy")
  181. {
  182. TakingDamage = true;
  183. StartCoroutine(DamageOverTime());
  184. }
  185.  
  186. if (collision.transform.tag == "Acid")
  187. {
  188. TakingDamage = true;
  189. StartCoroutine(DamageOverTime());
  190.  
  191. }
  192. }
  193.  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement