如何让预制体Trash保持递增速度?重复生成时速度不重置
问题描述
在Unity中重复生成名为“Trash”的对象时,期望它的速度能够持续递增,但每次生成新的Trash时,其速度都会回到初始值。如何实现让所有Trash对象始终使用持续递增的速度?
原Trash脚本代码
public class TrashScript : MonoBehaviour { float trashAcceleration = 2; float trashDespawn = -15; float trashInitialSpeed = 5; float trashProgressiveSpeed = 0; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { trashInitialSpeed += trashAcceleration * Time.deltaTime; trashProgressiveSpeed = trashInitialSpeed * Time.deltaTime; transform.position += (Vector3.left * trashProgressiveSpeed); if (transform.position.x < trashDespawn) { Destroy(gameObject); } } }
原生成Trash的Spawner代码
public class TrashSpawner : MonoBehaviour { public GameObject trash; public float heightOffset; public float spawnRate; private float timer = 0; // Update is called once per frame void Update() { if(timer < spawnRate) { timer += Time.deltaTime; } else { SpawnTrash(); timer = 0; } } void SpawnTrash() { float lowestPoint = transform.position.y - heightOffset; float highestPoint = transform.position.y + heightOffset; Instantiate(trash, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation); } }
问题原因
每个Trash实例都拥有独立的trashInitialSpeed变量,每次生成新Trash时,该变量会被重新初始化为5,导致新对象速度回到初始值,无法继承全局递增后的速度。
解决方案
将速度的全局状态统一放到TrashSpawner中管理,让所有Trash实例共享这个递增的速度值。以下提供两种实现逻辑:
逻辑1:所有Trash速度完全同步(全局统一递增)
让Trash直接读取Spawner中的全局速度,确保所有对象速度完全一致。
修改后的TrashSpawner脚本
public class TrashSpawner : MonoBehaviour { public static TrashSpawner Instance; public GameObject trash; public float heightOffset; public float spawnRate; public float globalTrashSpeed = 5f; public float trashAcceleration = 2f; private float timer = 0; void Awake() { Instance = this; } void Update() { // 持续递增全局速度 globalTrashSpeed += trashAcceleration * Time.deltaTime; if(timer < spawnRate) { timer += Time.deltaTime; } else { SpawnTrash(); timer = 0; } } void SpawnTrash() { float lowestPoint = transform.position.y - heightOffset; float highestPoint = transform.position.y + heightOffset; Instantiate(trash, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation); } }
修改后的TrashScript脚本
public class TrashScript : MonoBehaviour { float trashDespawn = -15; void Update() { // 直接使用Spawner中的全局速度 float trashProgressiveSpeed = TrashSpawner.Instance.globalTrashSpeed * Time.deltaTime; transform.position += (Vector3.left * trashProgressiveSpeed); if (transform.position.x < trashDespawn) { Destroy(gameObject); } } }
逻辑2:Trash继承当前全局速度后自行加速
每个Trash生成时继承当前全局速度,之后保持自身的加速度递增。
修改后的TrashSpawner脚本
public class TrashSpawner : MonoBehaviour { public GameObject trash; public float heightOffset; public float spawnRate; public float globalTrashSpeed = 5f; public float trashAcceleration = 2f; private float timer = 0; void Update() { // 持续递增全局速度 globalTrashSpeed += trashAcceleration * Time.deltaTime; if(timer < spawnRate) { timer += Time.deltaTime; } else { SpawnTrash(); timer = 0; } } void SpawnTrash() { float lowestPoint = transform.position.y - heightOffset; float highestPoint = transform.position.y + heightOffset; GameObject newTrash = Instantiate(trash, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation); // 将当前全局速度和加速度传递给新Trash TrashScript trashScript = newTrash.GetComponent<TrashScript>(); if(trashScript != null) { trashScript.Initialize(globalTrashSpeed, trashAcceleration); } } }
修改后的TrashScript脚本
public class TrashScript : MonoBehaviour { float trashDespawn = -15; float currentSpeed; float trashAcceleration; public void Initialize(float initialSpeed, float acceleration) { currentSpeed = initialSpeed; trashAcceleration = acceleration; } void Update() { // 自身继续加速 currentSpeed += trashAcceleration * Time.deltaTime; float trashProgressiveSpeed = currentSpeed * Time.deltaTime; transform.position += (Vector3.left * trashProgressiveSpeed); if (transform.position.x < trashDespawn) { Destroy(gameObject); } } }
内容的提问来源于stack exchange,提问作者Gonko
相关产品推荐
相关产品推荐

