小案例:
20240719_104639
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewEnemyIncubator : MonoBehaviour
{
public GameObject[] enemys;
private float time;//每波生成间隔
private float times;//波内生成间隔
private float count;//波数
private float counts;//每波数量
// Start is called before the first frame update
void Start()
{
time = 2;
times = 1;
count = 5;
counts = 4;
StartCoroutine(CreatEnemy());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator CreatEnemy()
{
for (int i = 0; i < count; i++)
{
for (int j = 0; j < counts; j++)
{
Instantiate(enemys[Random.Range(0, enemys.Length)],transform.position,Quaternion.identity);
yield return new WaitForSeconds(times);
}
yield return new WaitForSeconds(time);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NewEnemyAI : MonoBehaviour
{
private NavMeshAgent agent;
private Transform targetPos;
private Animator ani;
// Start is called before the first frame update
void Start()
{
ani = GetComponent<Animator>();
agent = this.GetComponent<NavMeshAgent>();
targetPos = GameObject.Find("EVE").transform.GetChild(0);
}
// Update is called once per frame
void Update()
{
agent.destination = targetPos.position;
if (agent.isStopped)
{
ani.SetBool("Run", false);
}
else
{
ani.SetBool("Run", true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class NewGameManage : MonoBehaviour
{
private GameObject selectPanel;
private GameObject firstPanel;
private GameObject nextSelectPanel;
public GameObject[] towers;//塔
private GameObject selectTower;//即将要创建的炮塔
private Transform basePos;//地基位置,炮塔的创建位置
// Start is called before the first frame update
void Start()
{
selectTower = null;
selectPanel = transform.Find("SelectCanvas").gameObject;//找到被隐藏的游戏物体
firstPanel = selectPanel.transform.GetChild(0).gameObject;
nextSelectPanel = selectPanel.transform.GetChild(1).gameObject;
Debug.Log(selectPanel);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
SelectBase();
}
private void SelectBase()//选择创建炮台的地基
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit,200)&&EventSystem.current.IsPointerOverGameObject()==false)
{
if (hit.transform.tag=="TowerBase")//点击了地基,就可以创建
{
//
ShowSelectPanel(hit.transform);
basePos = hit.transform;
}
}
}
private void ShowSelectPanel(Transform pos)
{
selectPanel.transform.SetParent(pos,false);
selectPanel.transform.localPosition = new Vector3(0, 7, 0);
selectPanel.transform.rotation = Quaternion.Euler(60, 0, 0);
selectPanel.SetActive(true);
}
//事件
public void SelectowerOne(bool isOn)
{
if (isOn)
{
Debug.Log("SelectowerOne");
selectTower = towers[0];
firstPanel.SetActive(false);
nextSelectPanel.SetActive(true);
}
}
public void SelectowerTwo(bool isOn)
{
if (isOn)
{
Debug.Log("SelectowerTwo");
selectTower = towers[1];
firstPanel.SetActive(false);
nextSelectPanel.SetActive(true);
}
}
public void SelectowerThree(bool isOn)
{
if (isOn)
{
Debug.Log("SelectowerThree");
selectTower = towers[2];
firstPanel.SetActive(false);
nextSelectPanel.SetActive(true);
}
}
public void CloseAll()//关闭所有(第一层)
{
selectPanel.SetActive(false);
nextSelectPanel.SetActive(false);
firstPanel.SetActive(true);
}
public void CloseNext()//第二层关闭
{
nextSelectPanel.SetActive(false);
firstPanel.SetActive(true);
}
public void CreatTower()//创建
{
Debug.Log("创建");
if (selectTower!=null)
{
GameObject tempTower = Instantiate(selectTower);
tempTower.transform.SetParent(basePos, false);
tempTower.transform.localPosition = Vector3.up * 2.5f;
//加炮塔的攻击脚本
tempTower.AddComponent<NewTowerAI>();
InItUI();
}
}
public void CellTower()//出售
{
Debug.Log("出售");
if (basePos.childCount>=2)
{
Destroy(basePos.GetChild(0).gameObject);
InItUI();
}
else
{
Debug.Log("目标地基没有炮塔。无法操作!");
}
}
private void InItUI()//初始化UI
{
selectTower = null;
selectPanel.SetActive(false);
firstPanel.SetActive(true);
nextSelectPanel.SetActive(false);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewTowerAI : MonoBehaviour
{
[HideInInspector]
public List<GameObject> enemy;//可以攻击的列表
private GameObject targetObject;//目标怪物
private float dis;
private Transform turret;//炮塔可以旋转的部位
private float times;
private GameObject bulletPrefab;
private Transform firePos;
// Start is called before the first frame update
void Start()
{
dis = 1000;
times = 0;
enemy = new List<GameObject>();
targetObject = null;
turret = transform.GetChild(0);
firePos = turret.GetChild(0);
bulletPrefab = Resources.Load<GameObject>("Prefabs/Bullet");
}
// Update is called once per frame
void Update()
{
Debug.Log(enemy.Count);
if (enemy.Count>0)
{
if (targetObject==null)
{
//选怪
targetObject = SelectTarget();
}
}
if (targetObject!=null)
{
//前奏攻击
LookTarget();
}
}
public void OnTriggerEnter(Collider other)//检测怪物刚进入攻击范围
{
if (other.tag=="Enemy")
{
if (!enemy.Contains(other.gameObject))
{
enemy.Add(other.gameObject);
}
}
}
public void OnTriggerExit(Collider other)//当怪物离开攻击范围
{
if (other.tag=="Enemy")
{
if (targetObject !=null&&other.name == targetObject.name)
{
targetObject = null;
}
if (enemy.Contains(other.gameObject))
{
enemy.Remove(other.gameObject);
}
}
}
private GameObject SelectTarget()//选怪
{
GameObject temp = null;
float distance = 0;
for (int i = 0; i <enemy.Count; i++)
{
if (enemy[i] != null) //原因是因为那个怪物死了 又被你的另一个塔给选中了 拿去算距离,这个时候报错说已经销毁了
{
distance = Vector3.Distance(transform.position, enemy[i].transform.position);
if (distance <= dis)
{
dis = distance;
temp = enemy[i];
}
}
}
return temp;
}
private void LookTarget()
{
Vector3 pos = targetObject.transform.position;
pos.y = turret.position.y;
turret.LookAt(pos);
times += Time.deltaTime;
if (times>=1)
{
//产生实际的攻击效果
Attack();
times = 0;
}
}
private void Attack()
{
GameObject bullet = Instantiate(bulletPrefab, firePos.position, Quaternion.identity);
//给子弹挂脚本
bullet.AddComponent<NewBulletMove>().target=targetObject;
bullet.transform.LookAt(targetObject.transform.position);
bullet.GetComponent<NewBulletMove>().script = this;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBulletMove : MonoBehaviour
{
public GameObject target = null;
public NewTowerAI script=null;
private float times;
// Start is called before the first frame update
void Start()
{
times = 3;
}
// Update is called once per frame
void Update()
{
times -= Time.deltaTime;
if (times<=0)
Destroy(gameObject);
transform.Translate(Vector3.forward * Time.deltaTime * 15);
Attack();
}
private void Attack()
{
if (target!=null)
{
if (Vector3.Distance(transform.position,target.transform.position)<1)
{
Destroy(target);
script.enemy.Remove(target);
Destroy(gameObject);
}
}
else
{
Destroy(gameObject);
}
}
}