我正在嘗試以棒球格式創(chuàng)建擊球游戲。我創(chuàng)建了一個球作為預(yù)制件。我想在一定時間內(nèi)把球推到主場景。例如; 當(dāng)?shù)谝粋€球在場景中時,第二個球會在 5-6 秒后生成,然后是第三個、第四個等等。我是 Unity 的初學(xué)者級別,我不擅長 C#。我不確定我是否使用了真正的函數(shù),例如 Instantiate。這是我的腳本:using System.Collections;using System.Collections.Generic;using UnityEngine;public class Ball : MonoBehaviour { public float RotateSpeed = 45; //The ball rotates around its own axis public float BallSpeed = 0.2f; public GameObject[] prefab; public Rigidbody2D rb2D; void Start() { rb2D = GetComponent<Rigidbody2D>(); //Get component attached to gameobject Spawn (); } void FixedUpdate() { rb2D.MoveRotation(rb2D.rotation + RotateSpeed * Time.fixedDeltaTime); //The ball rotates around its own axis rb2D.AddForce(Vector2.left * BallSpeed); InvokeRepeating("Spawn", 2.0f, 2.0f); } public void Spawn () { int prefab_num = Random.Range(0,3); Instantiate(prefab[prefab_num]); }}應(yīng)用這個腳本后,結(jié)果不是我想要的。
如何在 Unity 中的特定時間段內(nèi)生成預(yù)制件?
慕碼人8056858
2021-06-28 17:59:31