浮动会随着时间的推移而增加,但它不会保持在下一个产生的浮动上,所以对于前流星1,当它被摧毁时速度为-2.5,然后流星2,当它产生时速度为-2.5但我的游戏是让它们都以-2的速度繁殖这是推进器本身的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeteorMover : MonoBehaviour
{
public float speed = -2f;
Rigidbody2D rigidBody;
// Start is called before the first frame update
IEnumerator Start()
{
rigidBody = GetComponent<Rigidbody2D>();
// Give meteor an initial downward velocity
rigidBody.velocity = new Vector2(0, speed);
WaitForSeconds wait = new WaitForSeconds(1);
int time = 1;
while (time > 0)
{
speed -= .1f;
time++;
//yield return speed;
yield return wait;
}
//yield return speed;
yield return wait;
}
}
然后spawners脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeteorSpawn : MonoBehaviour
{
public GameObject meteorPrefab;
public float minSpawnDelay = 1f;
public float maxSpawnDelay = 3f;
public float spawnXLimit = 6f;
// Start is called before the first frame update
void Start()
{
Spawn();
}
void Spawn()
{
// Create a meteor at a random x position
float random = Random.Range(-spawnXLimit, spawnXLimit);
Vector3 spawnPos = transform.position + new Vector3(random, 0f, 0f);
Instantiate(meteorPrefab, spawnPos, Quaternion.identity);
Invoke("Spawn", Random.Range(minSpawnDelay, maxSpawnDelay));
}
}
问题是因为MeteorMover中有以下内容:
public float speed = -2f;
因此,预制件一创建,其速度就设置为-2f。当最后一颗流星被摧毁时,你需要获得它的速度,并将该速度用于实例化的下一颗流星。