

using UnityEngine; using System.Collections; public class PlasmaTurretAI : MonoBehaviour //       ,    MonoBehaviour    ""    GameObject. { //     void Start () { } //      void Update () { } }  using System.Collections.Generic; using System.Linq; using UnityEngine; //       ,    MonoBehaviour    ""    GameObject (     ). public class PlasmaTurretAI : MonoBehaviour { public GameObject[] targets; //   public GameObject curTarget; public float towerPrice = 100.0f; public float attackMaximumDistance = 50.0f; //  public float attackMinimumDistance = 5.0f; public float attackDamage = 10.0f; // public float reloadTimer = 2.5f; //  ,   public const float reloadCooldown = 2.5f; //  ,  public float rotationSpeed = 1.5f; //    public int FiringOrder = 1; //    (    2) public Transform turretHead; public RaycastHit Hit; //     private void Start() { turretHead = transform.Find("pushka"); //      } //      private void Update() { if (curTarget != null) //      { float distance = Vector3.Distance(turretHead.position, curTarget.transform.position); //    if (attackMinimumDistance < distance && distance < attackMaximumDistance) //          { turretHead.rotation = Quaternion.Slerp(turretHead.rotation, Quaternion.LookRotation(curTarget.transform.position - turretHead.position), rotationSpeed * Time.deltaTime); //     if (reloadTimer > 0) reloadTimer -= Time.deltaTime; //     -   if (reloadTimer < 0) reloadTimer = 0; //     -     if (reloadTimer == 0) //  { MobHP mhp = curTarget.GetComponent<MobHP>(); switch (FiringOrder) //,     { case 1: Debug.Log("  "); //   FiringOrder++; // FiringOrder  1 break; case 2: Debug.Log("  "); //   FiringOrder = 1; // FiringOrder    break; } reloadTimer = reloadCooldown; //        } } } else // { curTarget = SortTargets(); //     } } //    ,    ! public GameObject SortTargets() { float closestMobDistance = 0; //       GameObject nearestmob = null; //    List<GameObject> sortingMobs = GameObject.FindGameObjectsWithTag("Monster").ToList(); //     Monster      foreach (var everyTarget in sortingMobs) //     { //    ,  closestMobDistance    if ((Vector3.Distance(everyTarget.transform.position, turretHead.position) < closestMobDistance) || closestMobDistance == 0) { closestMobDistance = Vector3.Distance(everyTarget.transform.position, turretHead.position); //     ,     nearestmob = everyTarget;//    } } return nearestmob; //   } } 


 using UnityEngine; using System.Collections.Generic; public class MobAI : MonoBehaviour { public GameObject Target; //  public float mobPrice = 5.0f; //    public float mobMinSpeed = 0.5f; //   public float mobMaxSpeed = 2.0f; //   public float mobRotationSpeed = 2.5f; //   public float attackDistance = 5.0f; //  public float damage = 5; //,   public float attackTimer = 0.0f; //     public const float coolDown = 2.0f; //,         private float MobCurrentSpeed; // ,   private Transform mob; //    private GlobalVars gv; //     private void Awake() { gv = GameObject.Find("GlobalVars").GetComponent<GlobalVars>(); //  mob = transform; //     ( ) MobCurrentSpeed = Random.Range(mobMinSpeed, mobMaxSpeed); //         } private void Update() { if (Target == null) //    { Target = SortTargets(); //      } else //     { mob.rotation = Quaternion.Lerp(mob.rotation, Quaternion.LookRotation(new Vector3(Target.transform.position.x, 0.0f, Target.transform.position.z) - new Vector3(mob.position.x, 0.0f, mob.position.z)), mobRotationSpeed); //-,    ! mob.position += mob.forward * MobCurrentSpeed * Time.deltaTime; //  ,    float distance = Vector3.Distance(Target.transform.position, mob.position); //    Vector3 structDirection = (Target.transform.position - mob.position).normalized; //   float attackDirection = Vector3.Dot(structDirection, mob.forward); //   if (distance < attackDistance && attackDirection > 0) //         { if (attackTimer > 0) attackTimer -= Time.deltaTime; //    0 -   if (attackTimer <= 0) //         { TurretHP thp = Target.GetComponent<TurretHP>(); //     if (thp != null) thp.ChangeHP(-damage); //   ,   (      ,   ) attackTimer = coolDown; //     } } } } //    ,    ! private GameObject SortTargets() { float closestTurretDistance = 0; //       GameObject nearestTurret = null; //    List<GameObject> sortingTurrets = gv.TurretList; //    foreach (var turret in sortingTurrets) //     { //    ,  closestTurretDistance    if ((Vector3.Distance(mob.position, turret.transform.position) < closestTurretDistance) || closestTurretDistance == 0) { closestTurretDistance = Vector3.Distance(mob.position, turret.transform.position); //     ,     nearestTurret = turret;//    } } return nearestTurret; //   } }  using System.Collections.Generic; using UnityEngine; public class GlobalVars : MonoBehaviour { public List<GameObject> MobList = new List<GameObject>(); //    public int MobCount = 0; //    public List<GameObject> TurretList = new List<GameObject>(); //    public int TurretCount = 0; //    public float PlayerMoney = 200.0f; //  }  using UnityEngine; public class MobHP : MonoBehaviour { public float maxHP = 100; //  public float curHP = 100; //  public Color MaxDamageColor = Color.red; //   public Color MinDamageColor = Color.blue; //   private GlobalVars gv; //     private void Awake() { gv = GameObject.Find("GlobalVars").GetComponent<GlobalVars>(); //  if (gv != null) { gv.MobList.Add(gameObject); //      gv.MobCount++; //   } if (maxHP < 1) maxHP = 1; //      -   } public void ChangeHP(float adjust) //    { if ((curHP + adjust) > maxHP) curHP = maxHP;//     adjust   ,    -      else curHP += adjust; //   adjust } private void Update() { gameObject.renderer.material.color = Color.Lerp(MaxDamageColor, MinDamageColor, curHP / maxHP); //       .  :  -    ,  - . if (curHP <= 0) //       { MobAI mai = gameObject.GetComponent<MobAI>(); //   AI  if (mai != null && gv != null) gv.PlayerMoney += mai.mobPrice; //   -          Destroy(gameObject); //  } } private void OnDestroy() //  { if (gv != null) { gv.MobList.Remove(gameObject); //      gv.MobCount--; //     1 } } }  using UnityEngine; public class TurretHP : MonoBehaviour { public float maxHP = 100; //  public float curHP = 100; //  private GlobalVars gv; //     private void Awake() { gv = GameObject.Find("GlobalVars").GetComponent<GlobalVars>(); //  if (gv != null) { gv.TurretList.Add(gameObject); gv.TurretCount++; } if (maxHP < 1) maxHP = 1; } public void ChangeHP(float adjust) { if ((curHP + adjust) > maxHP) curHP = maxHP; else curHP += adjust; if (curHP > maxHP) curHP = maxHP; } private void Update() { if (curHP <= 0) { Destroy(gameObject); } } private void OnDestroy() { if (gv != null) { gv.TurretList.Remove(gameObject); gv.TurretCount--; } } }  using UnityEngine; public class SpawnerAI : MonoBehaviour { public int waveAmount = 5; //   1      public int waveNumber = 0; //   public float waveDelayTimer = 30.0F; //    public float waveCooldown = 20.0F; // (  !)    ,     public int maximumWaves = 500; //     public Transform Mob; //     Unity public GameObject[] SpawnPoints; //   private GlobalVars gv; //     private void Awake() { SpawnPoints = GameObject.FindGameObjectsWithTag("Spawnpoint"); //      gv = GameObject.Find("GlobalVars").GetComponent<GlobalVars>(); //  } private void Update() { if (waveDelayTimer > 0) // h     { if (gv != null) { if (gv.MobCount == 0) waveDelayTimer = 0; //     -     else waveDelayTimer -= Time.deltaTime; //   } } if (waveDelayTimer <= 0) //      { if (SpawnPoints != null && waveNumber < maximumWaves) //           { foreach (GameObject spawnPoint in SpawnPoints) //    { for (int i = 0; i < waveAmount; i++) // i    ,          { Instantiate(Mob, new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z + i * 10), Quaternion.identity); //  } if (waveCooldown > 5.0f) //    5  { waveCooldown -= 0.1f; //  0.1  waveDelayTimer = waveCooldown; //   } else // { waveCooldown = 5.0f; //     5  waveDelayTimer = waveCooldown; } if (waveNumber >= 50) // 50  { waveAmount = 10; //   10     } } waveNumber++; //   } } } }   switch (FiringOrder) //,     { case 1: if (mhp != null) mhp.ChangeHP(-attackDamage); //   FiringOrder++; // FiringOrder  1 break; case 2: if (mhp != null) mhp.ChangeHP(-attackDamage); //   FiringOrder = 1; // FiringOrder    break; }  return nearestmob;  return closestMobDistance > attackMaximumDistance ? null : nearestmob; 

 using UnityEngine; public class Graphic : MonoBehaviour { private GlobalVars gv; //     public Rect buyMenu; //   public Rect firstTower; //     public Rect secondTower; //     public Rect thirdTower; //     public Rect fourthTower; //     public Rect fifthTower; //     public Rect towerMenu; //    (/) public Rect towerMenuSellTower; //    public Rect towerMenuUpgradeTower; //    public Rect playerStats; //   public Rect playerStatsPlayerMoney; //     public GameObject plasmaTower; //  ,     public GameObject plasmaTowerGhost; //  ,     private RaycastHit hit; //   public LayerMask raycastLayers = 1; //    / - ,    private GameObject ghost; //     private void Awake() { gv = GameObject.Find("GlobalVars").GetComponent<GlobalVars>(); //  if (gv == null) Debug.LogWarning("gv variable is not initialized correctly in " + this); //  ,  gv  buyMenu = new Rect(Screen.width - 185.0f, 10.0f, 175.0f, Screen.height - 100.0f); //  ,   X, Y, , . X  Y       firstTower = new Rect(buyMenu.x + 12.5f, buyMenu.y + 30.0f, 150.0f, 50.0f); secondTower = new Rect(firstTower.x, buyMenu.y + 90.0f, 150.0f, 50.0f); thirdTower = new Rect(firstTower.x, buyMenu.y + 150.0f, 150.0f, 50.0f); fourthTower = new Rect(firstTower.x, buyMenu.y + 210.0f, 150.0f, 50.0f); fifthTower = new Rect(firstTower.x, buyMenu.y + 270.0f, 150.0f, 50.0f); playerStats = new Rect(10.0f, 10.0f, 150.0f, 100.0f); playerStatsPlayerMoney = new Rect(playerStats.x + 12.5f, playerStats.y + 30.0f, 125.0f, 25.0f); towerMenu = new Rect(10.0f, Screen.height - 60.0f, 400.0f, 50.0f); towerMenuSellTower = new Rect(towerMenu.x + 12.5f, towerMenu.y + 20.0f, 75.0f, 25.0f); towerMenuUpgradeTower = new Rect(towerMenuSellTower.x + 5.0f + towerMenuSellTower.width, towerMenuSellTower.y, 75.0f, 25.0f); } private void Update() { switch (gv.mau5tate) //    { case GlobalVars.ClickState.Placing: //      { if (ghost == null) ghost = Instantiate(plasmaTowerGhost) as GameObject; //    -       else // { Ray scrRay = Camera.main.ScreenPointToRay(Input.mousePosition); // ,         if (Physics.Raycast(scrRay, out hit, Mathf.Infinity, raycastLayers)) //        (..  ) { Quaternion normana = Quaternion.FromToRotation(Vector3.up, hit.normal); //    ghost.transform.position = hit.point; //          ghost.transform.rotation = normana; //    ,    ,    if (Input.GetMouseButtonDown(0)) //   { GameObject tower = Instantiate(plasmaTower, ghost.transform.position, ghost.transform.rotation) as GameObject; //     if (tower != null) gv.PlayerMoney -= tower.GetComponent<PlasmaTurretAI>().towerPrice; //    Destroy(ghost); //   gv.mau5tate = GlobalVars.ClickState.Default; //      } } } break; } } } private void OnGUI() { GUI.Box(buyMenu, "Buying menu"); //     buyMenu  ,   "" if (GUI.Button(firstTower, "Plasma Tower\n100$")) //      { gv.mau5tate = GlobalVars.ClickState.Placing; //    } if (GUI.Button(secondTower, "Pulse Tower\n155$")) //   { //action here } if (GUI.Button(thirdTower, "Beam Tower\n250$")) { //action here } if (GUI.Button(fourthTower, "Tesla Tower\n375$")) { //action here } if (GUI.Button(fifthTower, "Artillery Tower\n500$")) { //action here } GUI.Box(playerStats, "Player Stats"); GUI.Label(playerStatsPlayerMoney, "Money: " + gv.PlayerMoney + "$"); GUI.Box(towerMenu, "Tower menu"); if (GUI.Button(towerMenuSellTower, "Sell")) { //action here } if (GUI.Button(towerMenuUpgradeTower, "Upgrade")) { //action here } } }  using System.Collections.Generic; using UnityEngine; public class GlobalVars : MonoBehaviour { public List<GameObject> MobList = new List<GameObject>(); //    public int MobCount = 0; //    public List<GameObject> TurretList = new List<GameObject>(); //    public int TurretCount = 0; //    public float PlayerMoney; //  public ClickState mau5tate = ClickState.Default; //   public enum ClickState //    { Default, Placing, Selling, Upgrading } public void Awake() { PlayerMoney = PlayerPrefs.GetFloat("Player Money", 200.0f); //  ,        -   200$,     } public void OnApplicationQuit() { PlayerPrefs.SetFloat("Player Money", PlayerMoney); //     PlayerPrefs.Save(); } } Source: https://habr.com/ru/post/148410/
All Articles