一、伤害门,激光门,任何物体经过两个物体之间都会毁灭消失
主要由三个脚本:
1.产生创建脚本CreateCubes脚本,绘制射线检测的Door脚本和CubeMove移动脚本,CubeMove不用赋给任何物体
Door和CreateCubes都给了空物体,记得赋值 startObj;和endObj;
2.在Unity面板里Tag里添加Door,图上两个物体都标签Door,如果不是标签Door的就毁灭
3.两个门之间距离不宜太远
4.注意生成物体的高度,绘制的射线是以两个Door的中心点连接进行绘制的一条射线,高度不够的话会检测不到。
----------------------------------------------------------------------------------------------------------------------------------------
1、首先是Door脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour {
//定义起始物体和终点物体
public GameObject startObj;
public GameObject endObj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//检测射线碰撞的点
RaycastHit hit;
//绘制射线并返回碰撞点
if(Physics.Linecast(startObj.transform.position,endObj.transform.position,out hit))
{
//在Unity面板Tag添加自定义标签Door
if (hit.collider.gameObject.CompareTag("Door") ==false)
{
Destroy(hit.collider.gameObject);
}
}
}
}
2. CreateCubes脚本文件下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreatCubes : MonoBehaviour {
//计时器
float timer = 0.0f;
// Update is called once per frame
void Update () {
//累加时间
timer += Time.deltaTime;
//判断计时器到达3S后生成一个Cube
if (timer >= 3)
{
//计时器归零
timer -= 3;
//创建一个方块并放到原点
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
obj.transform.position = new Vector3(0, 1, 0);
//添加组件的方式
obj.AddComponent<CubeMove>();
}
}
}
3. CubeMove脚本文件下:
void Update () { //不断产生移动的方块往伤害门走
transform.Translate(transform.forward * Time.deltaTime);
}
二、球型检测,吃金币,吸铁石功能的实现(两个文件)
先看下吃掉吸铁石道具,接着吸附金币的示例图
1、吃道具,获得吸附金币的功能,收集金币使用碰撞检测
2、球体检测,用到CubeMove2和CoinMove ,添加标签,金币是吸铁石,在Tag面板里添加magnet
3、金币都添加刚体,去掉重力,勾选IsTagger ,如果用碰撞器则不需要勾选,此处调用碰撞回调函数和触发回调函数都可实现
4、全选几个小球面板在Inspector面板最下面的AddCompoent中搜索添加CoinMove脚本
-------------------------------------------------------------------------------------------------------------------------------
实现的流程:
1.两个文件XiCubeMove和CoinMove文件,XiCubeMove文件给了我们的代表玩家Player的红色方块,CoinMove给了小球们
2. Magnet 和金币都各自在Unity面板里添加标签,标签必须写对对应
3.Magnet和Coins都添加--刚体把重力的√去掉,都点击Is Trigger触发
4、金币目标是Player ,让小球不移动,Player会先吃掉Magnet,再往前走,走到小球那,小球会
挨个飞过去,销毁或者隐藏掉。
-------------------------------------------------------------------------------------------------------------------------------
到此上图的效果已经实现,下面是添加一些简单的常见游戏的UI相关的逻辑
-------------------------------------------------------------------------------------------------------------------------------
添加一些相关的UI上的点击事件,UI相关的游戏逻辑
1、添加计数器,判断是否吃完金币,显示胜利UI;
2、游戏的开始按钮,暂停,继续,重新加载的功能添加
3、游戏背景图的添加
---------------------------------------------------------------------------------------------------------------------------------
1.. XiCubeMove文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class XiCubeMove : MonoBehaviour {
//一开始让吸铁石为错误,即没有吸附功能
bool isMagnet = false;
//定义速度
public float speed;
//是否开始游戏
public bool isStart = false;
//定义开始按钮
public Button button;
//定义暂停、继续按钮
public Button stopButton;
//定义重新开始按钮
public Button reStartBtn;
//暂停文字修改
public Text textStop;
//定义背景图
public Image image;
//游戏胜利弹窗
public Image vicImage;
//计数器
public int count;
//金币总数
public int coinCount;
//是否暂停
public bool isStop=false;
void Start()
{
//游戏刚开始时一些对象的隐藏
stopButton.gameObject.SetActive(false);
vicImage.gameObject.SetActive(false);
reStartBtn.gameObject.SetActive(false);
//动态实例化金币总数
coinCount = GameObject.FindGameObjectsWithTag("Coin").Length;
}
void Update()
{
//判断是否开始游戏
if (isStart)
{
//让玩家自动的进行移动,朝前后或者左右
transform.Translate(transform.right * speed * Time.deltaTime);
}
//判断是否获得吸附能力
if (isMagnet)
{
//使用球形检测,设定范围来判断都有哪些物体
Collider[] cols = Physics.OverlapSphere(transform.position, 10);
//使用循环来对范围内的物体进行定位
foreach (var item in cols)
{
//对小球加标签进行判断
if (item.gameObject.CompareTag("Coin"))
{
//找到小球身上的脚本,把isMove设为true
item.GetComponent<CoinMove>().isMove = true;
}
}
}
}
void OnTriggerEnter(Collider other)//触发器
{
if (other.gameObject.tag == "Magnet")
{
Destroy(other.gameObject);
isMagnet = true;
}
if (other.gameObject.CompareTag("Coin"))
{
Destroy(other.gameObject);
//计数器加1并进行判断
count++;
if (count == coinCount)
{
vicImage.gameObject.SetActive(true);
isStart = false;
}
}
}
//开始按钮的响应方法
public void Button()
{
button.gameObject.SetActive(false);
stopButton.gameObject.SetActive(true);
reStartBtn.gameObject.SetActive(true);
isStart = true;
image.gameObject.SetActive(false);
}
//暂停按钮的方法
public void StopButton()
{
//通过设置bool值来调用时间的暂停来实现
if(isStop == false)
{
Time.timeScale = 0f;
textStop.text = "继续";
isStop = true;
}
else
{
Time.timeScale = 1f;
textStop.text = "暂停";
isStop = false;
}
//此处是通过把物体的行为暂停来实现一种假的暂停
//if (isStart == true || isMagnet == true)
//{
// isStart = false;
// isMagnet = false;
// textStop.text = "继续";
//}
//else if (isStart == false || isMagnet == false)
//{
// isStart = true;
// isMagnet = true;
// textStop.text = "暂停";
//}
}
//重新开始游戏按钮的响应方法
public void RestartButton()
{
//加载场景
UnityEngine.SceneManagement.SceneManager.LoadScene(0);
}
}
2. CoinMove文件:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinMove : MonoBehaviour {
//定义一个目标点
public Transform target;
//决定小球是否向玩家移动
public bool isMove = false;
void Update()
{
if (isMove)
{
//球朝向目标移动
transform.position = Vector3.Lerp(transform.position, target.position, 0.2f);
}
}
}
最后的总体效果大致如下: