Advertisement
Guest User

Untitled

a guest
Jan 17th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. public class Player : MonoBehaviour
  7. {
  8.     public float speed = 300f;
  9.     public int health = 100;
  10.     CharacterController controller;
  11.     float horizontalMovement = 0f;
  12.     bool jump = false;
  13.     bool jetpack = false;
  14.     public bool hasJetpack;
  15.     bool obtainedJetpack = false;
  16.     float jetpackFuel = 100f;
  17.  
  18.     public void Start()
  19.     {
  20.         controller = GetComponent<CharacterController>();
  21.         transform.Find("Jetpack").gameObject.SetActive(false);
  22.     }
  23.  
  24.     public void Update()
  25.     {
  26.         horizontalMovement = Input.GetAxisRaw("Horizontal") * speed;
  27.         var weapon = transform.GetChild(0).GetComponent<Weapon>();
  28.  
  29.         if (Input.GetButtonDown("Jump"))
  30.         {
  31.             jump = true;
  32.         }
  33.         if (hasJetpack)
  34.         {
  35.             obtainedJetpack = true;
  36.  
  37.             if (Input.GetButtonDown("Jump"))
  38.             {
  39.                 jetpack = true;
  40.                 transform.Find("Jetpack").GetChild(0).gameObject.SetActive(true);
  41.                 GetComponent<BetterJump>().enabled = false;
  42.             }
  43.             if (Input.GetButtonUp("Jump"))
  44.             {
  45.                 jetpack = true;
  46.                 transform.Find("Jetpack").GetChild(0).gameObject.SetActive(false);
  47.                 GetComponent<BetterJump>().enabled = true;
  48.             }
  49.             if (Input.GetButton("Jump"))
  50.             {
  51.                 jetpackFuel -= 0.075f;
  52.             }
  53.             if (jetpackFuel <= 0)
  54.             {
  55.                 hasJetpack = false;
  56.                 transform.Find("Jetpack").GetChild(0).gameObject.SetActive(false);
  57.             }
  58.         }
  59.  
  60.         if (Input.GetButtonDown("Shoot"))
  61.         {
  62.             if (weapon.bulletsLeftInMag > 0)
  63.             {
  64.                 InvokeRepeating("Shoot", 0f, transform.GetChild(0).GetComponent<Weapon>().fireSpeed);
  65.                 Debug.Log(jetpackFuel);
  66.             }
  67.         }
  68.         if (Input.GetButtonUp("Shoot"))
  69.         {
  70.             CancelInvoke("Shoot");
  71.         }
  72.         if (Input.GetButtonDown("Reload"))
  73.         {
  74.             weapon.GetComponent<Animator>().Play("Reload");
  75.             weapon.GetComponent<Weapon>().bulletsLeftInMag = 0;
  76.             Invoke("Reload", weapon.reloadTime);
  77.         }
  78.         if (controller.m_Grounded & obtainedJetpack)
  79.         {
  80.             Invoke("JetpackRefuel", 2.5f);
  81.         }
  82.         if (!hasJetpack)
  83.         {
  84.             transform.Find("Jetpack").GetComponent<SpriteRenderer>().color = new Color32(10, 0, 0, 255);
  85.         }
  86.  
  87.         if (weapon.bulletsLeftInMag < 1)
  88.         {
  89.             weapon.GetComponent<SpriteRenderer>().color = new Color32(255, 150, 150, 255);
  90.             CancelInvoke("Shoot");
  91.         }
  92.         else
  93.         {
  94.             weapon.GetComponent<SpriteRenderer>().color = new Color32(255, 255, 255, 255);
  95.         }
  96.  
  97.  
  98.         if(health < 1)
  99.         {
  100.             Destroy(gameObject);
  101.         }
  102.     }
  103.  
  104.     public void FixedUpdate()
  105.     {
  106.         controller.Move(horizontalMovement * Time.fixedDeltaTime, false, jump, jetpack);
  107.         jetpack = false;
  108.         jump = false;
  109.     }
  110.  
  111.  
  112.     public void Shoot()
  113.     {
  114.         var weapon = transform.GetChild(0).GetComponent<Weapon>();
  115.  
  116.         weapon.Fire();
  117.         GetComponent<Rigidbody2D>().AddForce(-Vector2.right * transform.localScale.x * weapon.recoil);
  118.     }
  119.  
  120.     public void Reload()
  121.     {
  122.         var weapon = transform.GetChild(0).GetComponent<Weapon>();
  123.         weapon.bulletsLeftInMag = weapon.magazineSize;
  124.     }
  125.  
  126.     public void JetpackRefuel()
  127.     {
  128.         hasJetpack = true;
  129.         jetpackFuel = 100f;
  130.         transform.Find("Jetpack").GetComponent<SpriteRenderer>().color = new Color32(150, 80, 20, 255);
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement