Unity3D基础28:Invoke计时函数与碰撞销毁

 

前文:https://blog.csdn.net/Jaihk662/article/details/86766196(物体实例化)

https://blog.csdn.net/Jaihk662/article/details/86766324(物体销毁)

前面已经实现了“物体随机刷新”,代码如下,利用flag全局变量使金币每15帧刷新一次

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinPrint : MonoBehaviour
{
    int flag;
    public GameObject Mycoin;
    void Start()
    {
        flag = 0;
    }
    void Update()
    {
        flag++;
        if (flag % 15 == 0)
        {
            float x, z;
            x = Random.Range(-5f, 4.2f);        //-5f和(float)-5效果一样
            z = Random.Range(-5f, 4.2f);
            GameObject.Instantiate(Mycoin, new Vector3(x, (float)0.05, z), Quaternion.identity);
        }
    }
}

那么假设不以帧计时,那么有没有专门的API可以实现计时呢?

一、Invoke函数

  • Invoke(string, float):在float秒后执行函数string,只会执行一次
  • InvokeRepeating(string,float1,float2):在float1秒后执行函数string,接下来每float2秒执行一次
  • CancelInvoke():使这个脚本中所有的Invoke调用失效

同等代码如下:除此之外,当按下C键之后,所有的刷新都会停止

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinPrint : MonoBehaviour
{
    public GameObject Mycoin;
    void Start()
    {
        InvokeRepeating("CreateCoin", 3, 0.3f);        //3秒之后开始出现金币,每0.3秒刷新一次
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.C))        //按下C键后不再生成金币
        {
            CancelInvoke();
        }
    }
    void CreateCoin()       //先封装成一个方法
    {
        float x, z;
        x = Random.Range(-5f, 4.2f);
        z = Random.Range(-5f, 4.2f);
        GameObject.Instantiate(Mycoin, new Vector3(x, (float)0.05, z), Quaternion.identity);
    }
}

Invoke 相关的函数都在 MonoBehaviour 类里面,我们所有写的脚本类都是 MonoBehaviour 类的子类,所以我们可以直接通过方法名来调用父类中的方法

 

二、碰撞销毁

当主角碰撞到金币时,金币应该马上被销毁,不然就可以“反复刷分”,这显然是不合理的

方法如下:因为Coin太多,而名字是唯一的,所以要先建立Tag标签

之后只需要编写代码实现:当主角与标签为Tag的物体发生碰撞的时候,销毁这个物体即可,如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Text1: MonoBehaviour
{
    private Transform myTransform;      //实例化Transform对象
    private Rigidbody myRigidbody;
    void Start()
    {
        myTransform = gameObject.GetComponent<Transform>();
        myRigidbody = gameObject.GetComponent<Rigidbody>();
    }
    // Update is called once per frame
    void Update()
    {
        if (!Input.GetKey(KeyCode.LeftControl))
        {
            if (Input.GetKeyDown(KeyCode.UpArrow))      //箭头上
            {
                //Debug.Log("刚体向前移动");
                myRigidbody.MovePosition(myTransform.position + (new Vector3(0, 0, 1)));
            }
            //……
        }
    }
    //……
    void OnCollisionEnter(Collision coll)       //发生碰撞
    {
        Debug.Log("sdsd");
        if(coll.gameObject.tag=="Coin")     //如果是和金币发生碰撞
        {
            GameObject.Destroy(coll.gameObject);        //销毁金币
        }
    }
}

除此之外,最好将主角方块的质量设的特别大,这样主角就基本不会发生碰撞效果

实现结果:

当然,还可以将碰撞改为触发,并移除金币的刚体组件,给金币加上自动旋转效果,金币脚本和效果如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinDest: MonoBehaviour
{
    public GameObject Mycoin;
    private Transform myTransform;
    void Start()
    {
        myTransform = gameObject.GetComponent<Transform>();
        GameObject.Destroy(gameObject, 8);
    }
    void Update()
    {
        myTransform.Rotate(new Vector3(0,0,1), 10f);        //自动旋转
    }
}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值