Advertisement
Guest User

Untitled

a guest
Aug 15th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.72 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Audio;
  6. using UnityEngine.UIElements;
  7.  
  8. public class PlayerController : MonoBehaviour
  9. {
  10.     public float rotationSpeed;
  11.     public float flightSpeed;
  12.     private Camera mainCamera;
  13.     private bool flying = false;
  14.     public GameObject bullet;
  15.     public GameObject bomb;
  16.     private float bulletFireDelay = .2f;
  17.     private float bulletCooldownRemaining;
  18.     private bool bulletCooldown = false;
  19.     private float bombFireDelay = 3f;
  20.     private float bombCooldownRemaining;
  21.     private bool bombCooldown = false;
  22.     private GameObject bulletSpawner;
  23.     private GameObject bombSpawner;
  24.     public GameObject explosion;
  25.     public ParticleSystem smoke;
  26.     public ParticleSystem fire;
  27.     public bool dead = false;
  28.     bool crashed = false;
  29.     private Rigidbody rb;
  30.     private DebrisSpawner debrisSpawner;
  31.     private float bombGrace = .3f;
  32.     private bool bombTouch = false;
  33.     private bool stalled = false;
  34.  
  35.     public float debugXAngle;
  36.     public float debugYAngle;
  37.  
  38.     // Start is called before the first frame update
  39.     void Start()
  40.     {
  41.         mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
  42.         bulletSpawner = GameObject.Find("BulletSpawner");
  43.         bombSpawner = GameObject.Find("BombSpawner");
  44.         bulletCooldownRemaining = bulletFireDelay;
  45.         rb = GetComponent<Rigidbody>();
  46.         debrisSpawner = GameObject.FindAnyObjectByType<DebrisSpawner>();
  47.     }
  48.  
  49.     // Update is called once per frame
  50.     void Update()
  51.     {
  52.         debugXAngle = transform.eulerAngles.x;
  53.         debugYAngle = transform.eulerAngles.y;
  54.  
  55.         if (!dead)
  56.         {
  57.             if (Input.GetKey(KeyCode.W) && flying && !stalled)
  58.             {
  59.                 transform.Rotate(-rotationSpeed * Time.deltaTime, 0, 0, Space.World);
  60.             }
  61.             else if (Input.GetKey(KeyCode.S) && flying && !stalled)
  62.             {
  63.                 transform.Rotate(rotationSpeed * Time.deltaTime, 0, 0, Space.World);
  64.             }
  65.  
  66.             if (Input.GetKey(KeyCode.D) && flightSpeed <= 20)
  67.             {
  68.                 flightSpeed += 10 * Time.deltaTime;
  69.  
  70.                 if (flightSpeed >= 15)
  71.                     flying = true;
  72.             }
  73.             else if (Input.GetKey(KeyCode.A) && flightSpeed >= 0)
  74.             {
  75.                 flightSpeed -= 10 * Time.deltaTime;
  76.             }
  77.  
  78.             if (Input.GetKeyDown(KeyCode.Q) && flying && !stalled)
  79.             {
  80.                 StartCoroutine("Flip");
  81.             }
  82.  
  83.             if (Input.GetKey(KeyCode.Space))
  84.             {
  85.                 Shoot();
  86.             }
  87.  
  88.             if (Input.GetKeyDown(KeyCode.E))
  89.             {
  90.                 BombsAway();
  91.             }
  92.         }
  93.  
  94.         if (dead && !crashed) //keep plane tangent to the balistic arc while dead and crashing
  95.         {
  96.             // Create a rotation that looks along the velocity vector
  97.             Quaternion lookRotation = Quaternion.LookRotation(rb.velocity);
  98.  
  99.             // Convert to Euler angles, modify to lock the y-axis rotation
  100.             Vector3 euler = lookRotation.eulerAngles;
  101.             euler.z = transform.rotation.eulerAngles.z; // Lock y-axis rotation
  102.  
  103.             // Convert back to Quaternion and apply to the transform
  104.             transform.rotation = Quaternion.Euler(euler);
  105.         }
  106.         else if (transform.position.y >= 55 || (flightSpeed <= 10 && flying) && !stalled)
  107.         {
  108.             Stall();
  109.         }
  110.         else if (stalled)
  111.         {
  112.             transform.Rotate(0f, 180f * Time.deltaTime, 0f, Space.World);
  113.             if(flightSpeed >= 15)
  114.             {
  115.                 rb.velocity = Vector3.zero;
  116.                 rb.angularVelocity = Vector3.zero;
  117.                 stalled = false;
  118.                 StartCoroutine("EndStall");
  119.             }
  120.         }
  121.         else //normal powered movement
  122.         {
  123.             if((System.Math.Abs(0 - transform.eulerAngles.y) < .001f) || (System.Math.Abs(180 - transform.eulerAngles.y) < .001f))
  124.                 transform.Translate(0, 0, flightSpeed * Time.deltaTime, Space.Self);
  125.         }
  126.  
  127.         if (bulletCooldown) //controls rate of gunfire
  128.         {
  129.             bulletCooldownRemaining -= Time.deltaTime;
  130.             if (bulletCooldownRemaining < 0)
  131.             {
  132.                 bulletCooldown = false;
  133.                 bulletCooldownRemaining = bulletFireDelay;
  134.             }
  135.         }
  136.  
  137.         if (bombCooldown) //controls rate of bomb use
  138.         {
  139.             bombCooldownRemaining -= Time.deltaTime;
  140.             if (bombCooldownRemaining < 0)
  141.             {
  142.                 bombCooldown = false;
  143.                 bombCooldownRemaining = bombFireDelay;
  144.             }
  145.         }
  146.  
  147.         if(bombTouch) //controls grace period between touching a bomb and actually impacting it
  148.         {
  149.             bombGrace -= Time.deltaTime;
  150.             if (bombGrace < -1)
  151.             {
  152.                 bombGrace = .3f;
  153.                 bombTouch = false;
  154.             }
  155.         }
  156.     }
  157.  
  158.     private void Stall()
  159.     {
  160.         flightSpeed = 0;
  161.         stalled = true;
  162.         rb.useGravity = true;
  163.         StartCoroutine("StartStall");
  164.     }
  165.  
  166.     IEnumerator StartStall()
  167.     {
  168.         float angle;
  169.         if (transform.eulerAngles.x >= 0 && transform.eulerAngles.x <= 90)
  170.             angle = 30 - transform.eulerAngles.x;
  171.         else
  172.             angle = 390 - transform.eulerAngles.x;
  173.  
  174.         if (transform.eulerAngles.z > 0)
  175.             angle *= -1f;
  176.  
  177.         iTween.RotateAdd(gameObject, iTween.Hash("x", angle, "time", 1f, "islocal", true));
  178.         yield return null;
  179.     }
  180.  
  181.     IEnumerator EndStall()
  182.     {
  183.         float angleYDelta;
  184.         float angleYTarget;
  185.         float angleZDelta;
  186.         float angleZTarget;
  187.  
  188.         if (transform.eulerAngles.y > 0 && transform.eulerAngles.y <= 180)
  189.         {
  190.             angleYDelta = 180 - transform.eulerAngles.y;
  191.             angleYTarget = 180;
  192.         }
  193.         else
  194.         {
  195.             angleYDelta = 360 - transform.eulerAngles.y;
  196.             angleYTarget = 0;
  197.         }
  198.  
  199.         if (transform.eulerAngles.z > 0 && transform.eulerAngles.z <= 180)
  200.         {
  201.             angleZDelta = 180 - transform.eulerAngles.z;
  202.             angleZTarget = 180;
  203.         }
  204.         else
  205.         {
  206.             angleZDelta = 360 - transform.eulerAngles.z;
  207.             angleZTarget = 0;
  208.         }
  209.  
  210.         //rb.velocity = Vector3.zero;
  211.         rb.angularVelocity = Vector3.zero;
  212.  
  213.         iTween.RotateAdd(gameObject, iTween.Hash("y", angleYDelta, "z", angleZDelta, "time", 1f, "islocal", true));
  214.        
  215.         Vector3 stableAngle = new Vector3(transform.eulerAngles.x, angleYTarget, angleZTarget);
  216.         transform.eulerAngles = stableAngle;
  217.         rb.velocity = Vector3.zero;
  218.         rb.angularVelocity = Vector3.zero;
  219.         rb.useGravity = false;
  220.  
  221.         yield return null;
  222.     }
  223.  
  224.         private void LateUpdate()
  225.     {
  226.         //track camera with player along z axis
  227.         if (transform.position.z > -215 && transform.position.z < 215)
  228.         {
  229.             mainCamera.transform.position = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, transform.position.z);
  230.         }  
  231.     }
  232.  
  233.     private void OnTriggerEnter(Collider other)
  234.     {
  235.         if (other.CompareTag("Ground"))
  236.         {
  237.             Crash();
  238.         }
  239.  
  240.         if(other.CompareTag("Bomb"))
  241.         {
  242.             bombTouch = true;
  243.         }
  244.  
  245.         else if (other.CompareTag("Building"))
  246.         {
  247.             Explode();
  248.             BuildingHealth bh = other.GetComponentInParent<BuildingHealth>();
  249.             bh.Explode();
  250.         }
  251.         else if (other.CompareTag("StorageTank"))
  252.         {
  253.             Explode();
  254.             StorageTankHealth sth = other.GetComponentInParent<StorageTankHealth>();
  255.             sth.Explode();
  256.         }
  257.         else if (!dead)
  258.         {
  259.             Explode();  
  260.         }
  261.     }
  262.  
  263.     private void OnTriggerStay(Collider other)
  264.     {
  265.         if (!dead)
  266.         {
  267.             if (bombTouch && bombGrace <= 0)
  268.             {
  269.                 Destroy(other.gameObject);
  270.                 Explode();
  271.             }
  272.         }
  273.     }
  274.  
  275.     private void Explode()
  276.     {
  277.         stalled = false;
  278.         Instantiate(explosion, (transform.forward * 2.5f) + transform.position, explosion.transform.rotation);
  279.         smoke.Play();
  280.         fire.Play();
  281.         dead = true;
  282.         rb.AddForce(transform.forward * flightSpeed, ForceMode.Impulse);
  283.         flightSpeed = 0;
  284.         rb.useGravity = true;
  285.         debrisSpawner.SpawnDebris(transform.position, 5, 3, 0);
  286.     }
  287.  
  288.     private void Crash()
  289.     {
  290.         stalled = false;
  291.         Instantiate(explosion, (transform.forward * 2.5f) + transform.position, explosion.transform.rotation);
  292.         smoke.Play();
  293.         fire.Play();
  294.         dead = true;
  295.         crashed = true;
  296.         rb.useGravity = false;
  297.         flightSpeed = 0;
  298.         rb.velocity = Vector3.zero;
  299.         debrisSpawner.SpawnDebris((transform.forward * 2.5f) + transform.position, 5, 3, 0);
  300.     }
  301.  
  302.     IEnumerator Flip()
  303.     {
  304.         iTween.RotateAdd(gameObject, iTween.Hash("z", 180, "islocal", true, "time", 1.0f, "easetype", iTween.EaseType.spring));
  305.         yield return null;
  306.     }
  307.  
  308.     private void Shoot()
  309.     {
  310.         if (!bulletCooldown)
  311.         {
  312.             Instantiate(bullet, bulletSpawner.transform.position, transform.rotation);
  313.             bulletCooldown = true;
  314.         }
  315.     }
  316.  
  317.     private void BombsAway()
  318.     {
  319.         if (!bombCooldown)
  320.         {
  321.             Instantiate(bomb, bombSpawner.transform.position, transform.rotation);
  322.             bombCooldown = true;
  323.         }
  324.     }
  325. }
  326.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement