通过使用Invoke()方法或者是InvokeRepeating()方法,就可以在一定时间后再次调用程序。
Invoke()方法可以设置在多少秒之后调用一次函数。
而InvokeRepeating()方法可以设置在多少秒后不断的调用函数。(这个更好用)
比如用定时器做一起小球弹射又落下又弹起又落下这样…这样一个过程,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class invoke : MonoBehaviour
{
public float speed = 3;
public float symbols = 1;
// Start is called before the first frame update
void Start()
{
this.InvokeRepeating("downUp", 2,2);
}
// Update is called once per frame
void Update()
{
float d=symbols*speed*Time.deltaTime;
this.transform.Translate(0, d, 0, Space.Self);
}
void downUp()
{
symbols *= -1;
}
}
可以看到,通过定时改变symbols的值,就可以让小球不断的做上下移动,看起来很像是小球在弹跳,当然,这种弹跳并不符合自然规律,但是