第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在 Unity 中的特定時間段內(nèi)生成預(yù)制件?

如何在 Unity 中的特定時間段內(nèi)生成預(yù)制件?

C#
慕碼人8056858 2021-06-28 17:59:31
我正在嘗試以棒球格式創(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é)果不是我想要的。
查看完整描述

3 回答

?
月關(guān)寶盒

TA貢獻1772條經(jīng)驗 獲得超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;

}


查看完整回答
反對 回復(fù) 2021-07-10
  • 3 回答
  • 0 關(guān)注
  • 326 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號