Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class Player : MonoBehaviour
- {
- public float speed = 300f;
- public int health = 100;
- CharacterController controller;
- float horizontalMovement = 0f;
- bool jump = false;
- bool jetpack = false;
- public bool hasJetpack;
- bool obtainedJetpack = false;
- float jetpackFuel = 100f;
- public void Start()
- {
- controller = GetComponent<CharacterController>();
- transform.Find("Jetpack").gameObject.SetActive(false);
- }
- public void Update()
- {
- horizontalMovement = Input.GetAxisRaw("Horizontal") * speed;
- var weapon = transform.GetChild(0).GetComponent<Weapon>();
- if (Input.GetButtonDown("Jump"))
- {
- jump = true;
- }
- if (hasJetpack)
- {
- obtainedJetpack = true;
- if (Input.GetButtonDown("Jump"))
- {
- jetpack = true;
- transform.Find("Jetpack").GetChild(0).gameObject.SetActive(true);
- GetComponent<BetterJump>().enabled = false;
- }
- if (Input.GetButtonUp("Jump"))
- {
- jetpack = true;
- transform.Find("Jetpack").GetChild(0).gameObject.SetActive(false);
- GetComponent<BetterJump>().enabled = true;
- }
- if (Input.GetButton("Jump"))
- {
- jetpackFuel -= 0.075f;
- }
- if (jetpackFuel <= 0)
- {
- hasJetpack = false;
- transform.Find("Jetpack").GetChild(0).gameObject.SetActive(false);
- }
- }
- if (Input.GetButtonDown("Shoot"))
- {
- if (weapon.bulletsLeftInMag > 0)
- {
- InvokeRepeating("Shoot", 0f, transform.GetChild(0).GetComponent<Weapon>().fireSpeed);
- Debug.Log(jetpackFuel);
- }
- }
- if (Input.GetButtonUp("Shoot"))
- {
- CancelInvoke("Shoot");
- }
- if (Input.GetButtonDown("Reload"))
- {
- weapon.GetComponent<Animator>().Play("Reload");
- weapon.GetComponent<Weapon>().bulletsLeftInMag = 0;
- Invoke("Reload", weapon.reloadTime);
- }
- if (controller.m_Grounded & obtainedJetpack)
- {
- Invoke("JetpackRefuel", 2.5f);
- }
- if (!hasJetpack)
- {
- transform.Find("Jetpack").GetComponent<SpriteRenderer>().color = new Color32(10, 0, 0, 255);
- }
- if (weapon.bulletsLeftInMag < 1)
- {
- weapon.GetComponent<SpriteRenderer>().color = new Color32(255, 150, 150, 255);
- CancelInvoke("Shoot");
- }
- else
- {
- weapon.GetComponent<SpriteRenderer>().color = new Color32(255, 255, 255, 255);
- }
- if(health < 1)
- {
- Destroy(gameObject);
- }
- }
- public void FixedUpdate()
- {
- controller.Move(horizontalMovement * Time.fixedDeltaTime, false, jump, jetpack);
- jetpack = false;
- jump = false;
- }
- public void Shoot()
- {
- var weapon = transform.GetChild(0).GetComponent<Weapon>();
- weapon.Fire();
- GetComponent<Rigidbody2D>().AddForce(-Vector2.right * transform.localScale.x * weapon.recoil);
- }
- public void Reload()
- {
- var weapon = transform.GetChild(0).GetComponent<Weapon>();
- weapon.bulletsLeftInMag = weapon.magazineSize;
- }
- public void JetpackRefuel()
- {
- hasJetpack = true;
- jetpackFuel = 100f;
- transform.Find("Jetpack").GetComponent<SpriteRenderer>().color = new Color32(150, 80, 20, 255);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement