Advertisement
Weezle

Untitled

Sep 20th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Hero : MonoBehaviour
  6. {
  7. public int nStar;
  8. public Transform myTransform;
  9. public Vector3 s;
  10. public int nammo=20;
  11. public Animator anim;
  12. public bool onair;
  13. public Vector3 pos;
  14. public float wstar;
  15. public float labeltimer;
  16. public float maxspeed=6f;
  17. bool sactive = true;
  18. public GameObject star;
  19. public CameraScript DNS;
  20. public float wanim = 7f;
  21. public bool shootb=false;
  22. public bool plusammo;
  23. public GameObject ammo;
  24. public GUI main;
  25. public int q = 0;
  26. float XaY;
  27. Rigidbody2D phy;
  28. // Use this for initialization
  29. void Start ()
  30. {
  31. myTransform = transform;
  32. wstar=Random.Range(40,100);
  33. s = myTransform.position;
  34. anim = GetComponent<Animator> ();
  35. phy = GetComponent <Rigidbody2D> ();
  36. pos = Camera.main.WorldToScreenPoint(transform.position);
  37. DNS = Camera.main.GetComponent<CameraScript> ();
  38. star = GameObject.FindGameObjectWithTag ("star");
  39. star.SetActive (false);
  40. }
  41. // Update is called once per frame
  42. void Update ()
  43. {
  44. shootb = false;
  45. if (wstar >= 0)
  46. wstar -= Time.deltaTime * 10;
  47. if (wstar<0) {
  48. star.SetActive (true);
  49. }
  50. XaY = Input.GetAxis ("Horizontal");
  51. shootb = Input.GetKeyDown (KeyCode.E);
  52. if (shootb && nammo>0) {
  53. nammo--;
  54. GameObject huy = Instantiate (ammo, new Vector2 (transform.position.x + 0.5f, transform.position.y), transform.rotation) as GameObject;
  55. wanim = 7f;
  56. }
  57. if (shootb | wanim > 0 && nammo>0)
  58. if (wanim < 0f)
  59. wanim = 7f;
  60. else {
  61. wanim -= Time.deltaTime * 10;
  62. anim.Play ("att");
  63. }
  64. else {
  65. if (XaY != 0) {
  66. anim.Play ("Run");
  67. Run ();
  68. }
  69. else {
  70. anim.Play ("Idle");
  71. }
  72. }
  73. if (Input.GetKeyDown (KeyCode.Space) && !onair)
  74. {
  75. Jump ();
  76. onair = true;
  77. }
  78. if (plusammo) {
  79. labeltimer -= Time.deltaTime * 40f;
  80. if (labeltimer < 0)
  81. {
  82. labeltimer = 100f;
  83. plusammo = false;
  84. }
  85. }
  86. }
  87. void Run()
  88. {
  89. myTransform.Translate (Vector2.right * Time.deltaTime * maxspeed);
  90. if (XaY >= 0)
  91. transform.eulerAngles = new Vector3 (0, 0, 0);
  92. else
  93. transform.eulerAngles = new Vector3 (0, 180, 0);
  94. }
  95. void Jump()
  96. {
  97. phy.AddForce (Vector2.up * 30);
  98. }
  99. void OnCollisionEnter2D()
  100. {
  101. onair = false;
  102. }
  103. GameObject OnTriggerEnter2D (Collider2D myCol)
  104. {
  105. if (myCol.tag == "enemy") {
  106. myTransform.position = s;
  107. DNS.hp--;
  108. }
  109. if (myCol.tag == "star") {
  110. GameObject e = myCol.gameObject;
  111. DNS.nStar++;
  112. e.SetActive(false);
  113. wstar = 20f;
  114. sactive = false;
  115. star = e;
  116. }
  117. if (myCol.tag == "boxammo") {
  118. q = Random.Range (20, 40);
  119. nammo += q;
  120. plusammo = true;
  121. labeltimer = 100f;
  122. Destroy (myCol.gameObject);
  123. }
  124. return (star);
  125. }
  126. void OnGUI()
  127. {
  128. if (plusammo) {
  129. GUI.color = new Color (1, 1, 1, labeltimer / 100);
  130. GUI.Label (new Rect (pos.x - 50, pos.y - 50, 100, 100), "Ammo+" + q);
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement