LegitTeddyBears

Shooting System

May 31st, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ShootingSystem : MonoBehaviour
  6. {
  7.     public float fireRate;
  8.     public int damage;
  9.     public float fieldOfView;
  10.     public bool beam;
  11.     public GameObject projectile;
  12.     public List<GameObject> projectileSpawns;
  13.  
  14.     List<GameObject> m_lastProjectiles = new List<GameObject>();
  15.     float m_fireTimer = 0.0f;
  16.     public GameObject m_target;
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.         if (!m_target)
  22.         {
  23.             if (beam)
  24.                 RemoveLastProjectiles();
  25.  
  26.             return;
  27.         }
  28.  
  29.         if (beam && m_lastProjectiles.Count <= 0)
  30.         {
  31.             float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(m_target.transform.position - transform.position));
  32.  
  33.             if (angle < fieldOfView)
  34.             {
  35.                 SpawnProjectiles();
  36.             }
  37.         }
  38.         else if (beam && m_lastProjectiles.Count > 0)
  39.         {
  40.             float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(m_target.transform.position - transform.position));
  41.  
  42.             if (angle > fieldOfView)
  43.             {
  44.                 RemoveLastProjectiles();
  45.             }
  46.         }
  47.         else
  48.         {
  49.             m_fireTimer += Time.deltaTime;
  50.  
  51.             if (m_fireTimer >= fireRate)
  52.             {
  53.                 float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(m_target.transform.position - transform.position));
  54.  
  55.                 if (angle < fieldOfView)
  56.                 {
  57.                     SpawnProjectiles();
  58.  
  59.                     m_fireTimer = 0.0f;
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65.     void SpawnProjectiles()
  66.     {
  67.         if (!projectile)
  68.         {
  69.             return;
  70.         }
  71.  
  72.         m_lastProjectiles.Clear();
  73.  
  74.         for (int i = 0; i < projectileSpawns.Count; i++)
  75.         {
  76.             if (projectileSpawns[i])
  77.             {
  78.                 GameObject proj = Instantiate(projectile, projectileSpawns[i].transform.position, projectileSpawns[i].transform.rotation/*Quaternion.Euler(projectileSpawns[i].transform.forward)*/) as GameObject;
  79.                 proj.GetComponent<BaseProjectile>().FireProjectile(projectileSpawns[i], m_target, damage, fireRate);
  80.  
  81.                 m_lastProjectiles.Add(proj);
  82.             }
  83.         }
  84.     }
  85.  
  86.     public void SetTarget(GameObject target)
  87.     {
  88.         m_target = target;
  89.     }
  90.  
  91.     void RemoveLastProjectiles()
  92.     {
  93.         while (m_lastProjectiles.Count > 0)
  94.         {
  95.             Destroy(m_lastProjectiles[0]);
  96.             m_lastProjectiles.RemoveAt(0);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment