我正在尝试获得一个背景,以便开始越来越快地向左移动。是否在考虑使用两个值,一个表示要增加的百分比,另一个表示两次递增之间的间隔,该值也会随着间隔命中次数的增加而变得更大?
public float interval = 1; // 1 second between intervals starting off
public float speed = 2; // the starting speedi
void Start () {
// move left
GetComponent<Rigidbody2D>().velocity = Vector2.left * speed;
}
void Update () {
// check if interval has been reached? How?
//if interval has been reached then ( This does not work for me..
GetComponent<Rigidbody2D>().velocity = GetComponent<Rigidbody2D>().velocity * 0.01f;
interval = interval * 2;
}发布于 2015-12-21 12:14:35
我认为这里有几个问题需要解决:
1.)你每一帧都会将速度乘以.01,这实际上会完全冻结它。我不知道你这样做是什么意思。
2.)你每一帧都会调用GetComponent>Rigidbody2D<(),这是非常昂贵的!您应该为它创建一个类似以下内容的变量:
Rigidbody2d RB;
void Start () {
RB = GetComponent<Rigidbody2D>();
}对于如何在每个间隔后提高速度,我可能会声明一个计数器,您可以在每个帧中增加该计数器。如果计数器>间隔,则间隔*= 2且计数器=0
发布于 2015-12-22 03:33:36
首先,在一个变量中获取一次Rigidbody2D,然后使用它。
您可以使用Coroutine来实现此目的。
public float interval = 1; // 1 second between intervals starting off
public float speed = 2; // the starting speed
Rigidbody2D _rb;
void Start () {
// move left
_rb = GetComponent<Rigidbody2D>();
_rb.velocity = Vector2.left * speed;
StartCoroutine("IncreaseSpeedWithInterval");
}
void Update () {
}
IEnumerator IncreaseSpeedWithInterval()
{
while(true){
yield return new WaitForSeconds(interval);
// Now either Multiply your velocity by 1.01f or Add by 0.01f
_rb.velocity *= 1.01f;
// ========== OR ========== //
_rb.velocity += (Vector2.one * 0.01f);
}
}https://stackoverflow.com/questions/34387774
复制相似问题