军队
效果展示
克隆
克隆脚本1——克隆士兵
首先要建立士兵的预制体,该脚本用于预制体的克隆
using System.Collections;
using UnityEngine;
public class CloneTest1 : MonoBehaviour
{
public GameObject cloneObj;//被克隆的物体
private float xPos = 0f;//x坐标
private float zPos = 0f;//z坐标
/* public int rows = 10; // 行数
public int columns = 10; // 列数
public float spacing = 1.5f; // 士兵之间的间隔*/
private void Start()
{
if(cloneObj==null)
{
Debug.LogError("请将制作好的预制体拖入参数栏");
}
//启动协程
StartCoroutine(TodoSomeThingByTime());
//克隆代码
/*Instantiate(cloneObj,new(0f,0f,0f),Quaternion.identity);
Instantiate(cloneObj,new(1.5f,0f,0f),Quaternion.identity);
Instantiate(cloneObj, new(0f, 0f, 1.5f), Quaternion.identity);*/
/* for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
// 计算每个士兵的位置
Vector3 position = new Vector3(col * spacing, 0, row * spacing) + transform.position;
// 实例化预制体
Instantiate(cloneObj, position, Quaternion.identity, transform);
}
}*/
IEnumerator TodoSomeThingByTime()
{
for (int i = 1; i <= 40; i++)
{
//克隆代码
Instantiate(cloneObj, new Vector3(xPos, 0f, zPos), Quaternion.identity);
xPos += 1.5f;
if (i % 5 == 0)
{
zPos += 1.5f;
xPos = 0;
}
//等待0.2秒,继续运行(携程)
yield return new WaitForSeconds(0.2f);
}
}
}
}
克隆脚本2——克隆老虎
建立老虎的预制体,该脚本用于预制体的克隆
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class CloneTest2 : MonoBehaviour
{
public GameObject cloneObj2;//被克隆的物体
[SerializeField] private int rows; // 行数
[SerializeField] private int columns ; // 列数
[SerializeField] private float spacing = 1.5f; // 老虎之间的间隔
private void Start()
{
if(cloneObj2==null)
{
Debug.LogError("请将制作好的预制体拖入参数栏");
}
//启动协程
StartCoroutine(TodoSomeThingByTime());
//克隆代码
/* Instantiate(cloneObj,new(0f,0f,0f),Quaternion.identity);
Instantiate(cloneObj,new(1.5f,0f,0f),Quaternion.identity);
Instantiate(cloneObj, new(0f, 0f, 1.5f), Quaternion.identity);*/
IEnumerator TodoSomeThingByTime()
{
for (int row = 1; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
// 计算每个老虎的位置
Vector3 position = new Vector3(col * spacing, 0, row * spacing) + cloneObj2.transform.position;
// 实例化预制体
Instantiate(cloneObj2, position, Quaternion.Euler(0, 180, 0), transform);
// transform.Rotate(0, 180, 0);
//等待两秒,继续运行(携程)
yield return new WaitForSeconds(0.2f);
}
}
}
}
private void Update()
{
}
}
用到了协程,实现让克隆体一个一个克隆出来的效果,间隔为0.2秒
在yield return new WaitForSeconds(0.2f);中设定
克隆脚本2同上
using UnityEngine;
public class Move : MonoBehaviour
{
float min = 0.5f;
float max = 5.0f;
float randomMoveSpeed;
void Start()
{
randomMoveSpeed = Random.Range(min, max);
}
void Update()
{
transform.Translate(Vector3.forward * randomMoveSpeed * Time.deltaTime);
}
//只在游戏开始前运行一次
}
随机速度
每个克隆体的速度不一样,初始随机分配一个随机速度
using UnityEngine;
public class Move : MonoBehaviour
{
float min = 0.5f;
float max = 5.0f;
float randomMoveSpeed;
void Start()
{
randomMoveSpeed = Random.Range(min, max);
}
void Update()
{
transform.Translate(Vector3.forward * randomMoveSpeed * Time.deltaTime);
}
//只在游戏开始前运行一次
}
碰撞脚本
给老虎设置标签,分类一下
把碰撞脚本拖入士兵的预制体里
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Soldier : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
// 检查碰撞对象
if (other.gameObject.CompareTag("Tiger"))
{
// 隐藏当前士兵
gameObject.SetActive(false);
// 销毁当前士兵
Destroy(gameObject);
// 隐藏碰撞的老虎
other.gameObject.SetActive(false);
// 销毁碰撞的老虎
Destroy(other.gameObject);
}
}
private void OnTriggerStay(Collider other)
{
// 检查碰撞对象
if (other.gameObject.CompareTag("Tiger"))
{
// 隐藏当前士兵
gameObject.SetActive(false);
// 销毁当前士兵
Destroy(gameObject);
// 隐藏碰撞的老虎
other.gameObject.SetActive(false);
// 销毁碰撞的老虎
Destroy(other.gameObject);
}
}
}
游戏管理
时间管理+胜负管理
时间管理用协程,30秒时游戏暂停
胜负管理用到了标签查找,最后进行数值判断,进行相应的UI界面弹出
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using static UnityEditor.Experimental.AssetDatabaseExperimental.AssetDatabaseCounters;
public class GameManager : MonoBehaviour
{
private string tagToFindT = "Tiger";
private string tagToFindS = "Soldier";
[SerializeField]private GameObject gameOverPanelTigerWin;
[SerializeField]private GameObject gameOverPanelSoldierWin;
private void Start()
{
// 游戏开始时启动定时器
StartCoroutine(GameTimer(30));
}
IEnumerator GameTimer(float time)
{
// 等待游戏时间结束
yield return new WaitForSeconds(time);
// 暂停游戏时间
Time.timeScale = 0;
// 时间到达,判定胜负
EndGame();
}
void EndGame()
{
//GameObject myObject = GameObject.FindWithTag("Tiger");
GameObject[] objectsWithTigerTag = GameObject.FindGameObjectsWithTag(tagToFindT);
GameObject[] objectsWithSoldierTag = GameObject.FindGameObjectsWithTag(tagToFindS);
if (objectsWithTigerTag != null)
{
// 找到了对象
// objectsWithTag数组的长度就是带有指定标签的对象的个数
int countT = objectsWithTigerTag.Length;
int countS = objectsWithSoldierTag.Length;
// 打印出找到的对象个数
Debug.Log("Found " + countT + " objects with Tigertag " + tagToFindT);
Debug.Log("Found " + countS + " objectsWithSoldierTag " + tagToFindS);
// 根据剩余数显示游戏结束面板
if (countT > countS)
gameOverPanelTigerWin.SetActive(true);
if (countT < countS)
gameOverPanelSoldierWin.SetActive(true);
}
else
{
// 没有找到带有指定标签的对象
Debug.Log(0);
}
// 在这里添加更多游戏结束的逻辑,比如显示胜负界面、重置游戏状态、返回主菜单等。
}
}
相机入场动画
添加旧的动画组件
然后把动画掉调出来
注意一定要选中要编辑的摄像机
添加动画,添加属性,并设置达到想要的效果
UI界面
开始界面(不同场景跳转)
添加场景,加入跳转代码
把它绑到空物体中执行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class UI : MonoBehaviour
{
public void Jump()
{
SceneManager.LoadScene(1);
}
}
一个丑丑的界面be like
建立一个按钮,把脚本挂到鼠标点击事件里
结算界面
要把他们都隐藏,谁胜利了显示谁
两个很丑的界面be like
坦克
玩家移动脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Update()
{
//水平轴
float horizontal = Input.GetAxis("Horizontal");
//垂直轴
float vertical = Input.GetAxis("Vertical");
//向量
Vector3 dir = new Vector3(horizontal, 0, vertical);
//朝向量方向移动
transform.Translate(dir * 8 * Time.deltaTime);
}
}
子弹
子弹生成
每单击鼠标左键一下,生成一个子弹
需要找到炮口的位置(fire.position)
把炮口挂进去(跟挂物体一样的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMove : MonoBehaviour
{
[SerializeField] GameObject bullet;
[SerializeField] Transform fire;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(bullet, fire.position, transform.rotation);
}
}
}
拖尾组件
挂在子弹预制体上,会产生一个拖尾效果
子弹速度设置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletMove : MonoBehaviour
{
void Update()
{
transform.position += transform.forward * Time.deltaTime * 6;
}
}