Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Hero : MonoBehaviour
- {
- public int nStar;
- public Transform myTransform;
- public Vector3 s;
- public int nammo=20;
- public Animator anim;
- public bool onair;
- public Vector3 pos;
- public float wstar;
- public float labeltimer;
- public float maxspeed=6f;
- bool sactive = true;
- public GameObject star;
- public CameraScript DNS;
- public float wanim = 7f;
- public bool shootb=false;
- public bool plusammo;
- public GameObject ammo;
- public GUI main;
- public int q = 0;
- float XaY;
- Rigidbody2D phy;
- // Use this for initialization
- void Start ()
- {
- myTransform = transform;
- wstar=Random.Range(40,100);
- s = myTransform.position;
- anim = GetComponent<Animator> ();
- phy = GetComponent <Rigidbody2D> ();
- pos = Camera.main.WorldToScreenPoint(transform.position);
- DNS = Camera.main.GetComponent<CameraScript> ();
- star = GameObject.FindGameObjectWithTag ("star");
- star.SetActive (false);
- }
- // Update is called once per frame
- void Update ()
- {
- shootb = false;
- if (wstar >= 0)
- wstar -= Time.deltaTime * 10;
- if (wstar<0) {
- star.SetActive (true);
- }
- XaY = Input.GetAxis ("Horizontal");
- shootb = Input.GetKeyDown (KeyCode.E);
- if (shootb && nammo>0) {
- nammo--;
- GameObject huy = Instantiate (ammo, new Vector2 (transform.position.x + 0.5f, transform.position.y), transform.rotation) as GameObject;
- wanim = 7f;
- }
- if (shootb | wanim > 0 && nammo>0)
- if (wanim < 0f)
- wanim = 7f;
- else {
- wanim -= Time.deltaTime * 10;
- anim.Play ("att");
- }
- else {
- if (XaY != 0) {
- anim.Play ("Run");
- Run ();
- }
- else {
- anim.Play ("Idle");
- }
- }
- if (Input.GetKeyDown (KeyCode.Space) && !onair)
- {
- Jump ();
- onair = true;
- }
- if (plusammo) {
- labeltimer -= Time.deltaTime * 40f;
- if (labeltimer < 0)
- {
- labeltimer = 100f;
- plusammo = false;
- }
- }
- }
- void Run()
- {
- myTransform.Translate (Vector2.right * Time.deltaTime * maxspeed);
- if (XaY >= 0)
- transform.eulerAngles = new Vector3 (0, 0, 0);
- else
- transform.eulerAngles = new Vector3 (0, 180, 0);
- }
- void Jump()
- {
- phy.AddForce (Vector2.up * 30);
- }
- void OnCollisionEnter2D()
- {
- onair = false;
- }
- GameObject OnTriggerEnter2D (Collider2D myCol)
- {
- if (myCol.tag == "enemy") {
- myTransform.position = s;
- DNS.hp--;
- }
- if (myCol.tag == "star") {
- GameObject e = myCol.gameObject;
- DNS.nStar++;
- e.SetActive(false);
- wstar = 20f;
- sactive = false;
- star = e;
- }
- if (myCol.tag == "boxammo") {
- q = Random.Range (20, 40);
- nammo += q;
- plusammo = true;
- labeltimer = 100f;
- Destroy (myCol.gameObject);
- }
- return (star);
- }
- void OnGUI()
- {
- if (plusammo) {
- GUI.color = new Color (1, 1, 1, labeltimer / 100);
- GUI.Label (new Rect (pos.x - 50, pos.y - 50, 100, 100), "Ammo+" + q);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement