简体   繁体   English

如何在特定时间内在Unity中生成预制件?

[英]How to spawn prefabs within a certain period of time in Unity?

I am trying to create a ball hitting game in the baseball format. 我正在尝试以棒球形式创建击球游戏。 I create a ball as a prefab. 我创建一个球作为预制件。 I want to push the ball to the main scene within a certain period of time. 我想在一定时间内将球推到主要位置。

For example; 例如; when the first ball is in the scene, the second ball will spawn after 5-6 seconds, then the third, fourth etc. I am the beginner level of Unity and I am not good at C#. 当第一个球出现在场景中时,第二个球将在5-6秒后生成,然后是第三,第四等等。我是Unity的初学者,我不擅长C#。 I am not sure whether I am using the true functions such as Instantiate. 我不确定我是否使用了诸如Instantiate之类的真正功能。 Here is my script: 这是我的脚本:

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]);
    }

}

After I apply this script, the result is not what I want. 应用此脚本后,结果不是我想要的。

在此处输入图片说明

Add InvokeRepeating("Spawn", 2.0f, 2.0f); 添加InvokeRepeating("Spawn", 2.0f, 2.0f); to the Start not the FixedUpdate . Start而不是FixedUpdate InvokeRepeating invokes the method methodName in time seconds, then repeatedly every repeatRate seconds. InvokeRepeating以秒为单位调用方法methodName ,然后每隔repeatRate秒重复一次。 You can check the documentation here . 您可以在此处查看文档。

Use Coroutines 使用协程

private IEnumerator SpawnBall() {
    while(true) {
        Instantiate(baseball);
        yield return new WaitForSeconds(5);
    }
}

Which can then be started with StartCoroutine() and terminated in one of three ways: 然后可以使用StartCoroutine()启动它,并可以通过StartCoroutine()三种方式之一终止它:

  • internally by breaking out of the while loop with break (the function would then have no more lines to execute and exit) 在内部通过带有break的while循环break (该函数将没有更多的行可以执行和退出)
  • internally by yield break 在内部通过yield break
  • externally by calling StopCoroutine() on a reference to the coroutine 通过在对协程的引用上调用StopCoroutine()在外部进行

Alternative to the other answers: Just use a countdown. 其他答案的替代方法:只需使用倒数计时即可。 This sometimes gives you more control 有时这可以让您更好地控制

// 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;
}

I updated my script by considering your feedbacks and it works like a charm. 我通过考虑您的反馈来更新脚本,它的工作原理很吸引人。 Thanks to all! 谢谢大家!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;

public class Ball : MonoBehaviour {
    public float RotateSpeed = 45; //The ball rotates around its own axis
    public float BallSpeed = 0.2f; 

    public GameObject BaseBall;
    public Transform BallLocation;

    public Rigidbody2D Ball2D;

    void Start() {
        Ball2D = GetComponent<Rigidbody2D>(); //Get component attached to gameobject
        InvokeRepeating("Spawn", 5.0f, 150f);
    }       

    void FixedUpdate() {
        Ball2D.MoveRotation(Ball2D.rotation + RotateSpeed * Time.fixedDeltaTime); //The ball rotates around its own axis
        Ball2D.AddForce(Vector2.left * BallSpeed);
    }

    public void Spawn () 
    {
        Instantiate (BaseBall, BallLocation.position, BallLocation.rotation);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM