亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Unity 中的特定時間段內生成預制件?

如何在 Unity 中的特定時間段內生成預制件?

C#
慕碼人8056858 2021-06-28 17:59:31
我正在嘗試以棒球格式創建擊球游戲。我創建了一個球作為預制件。我想在一定時間內把球推到主場景。例如; 當第一個球在場景中時,第二個球會在 5-6 秒后生成,然后是第三個、第四個等等。我是 Unity 的初學者級別,我不擅長 C#。我不確定我是否使用了真正的函數,例如 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]);    }}應用這個腳本后,結果不是我想要的。
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

其他答案的替代方案:只需使用倒計時。這有時會給你更多的控制


// Set your offset here (in seconds)

float timeoutDuration = 2;


float timeout = 2;


void Update()

{

    if(timeout > 0)

    {

        // Reduces the timeout by the time passed since the last frame

        timeout -= Time.deltaTime;


        // return to not execute any code after that

        return;

    }


    // this is reached when timeout gets <= 0


    // Spawn object once

    Spawn();


    // Reset timer

    timeout = timeoutDuration;

}


查看完整回答
反對 回復 2021-07-10
  • 3 回答
  • 0 關注
  • 350 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號