繁体   English   中英

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

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

我正在尝试以棒球形式创建击球游戏。 我创建一个球作为预制件。 我想在一定时间内将球推到主要位置。

例如; 当第一个球出现在场景中时,第二个球将在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]);
    }

}

应用此脚本后,结果不是我想要的。

在此处输入图片说明

添加InvokeRepeating("Spawn", 2.0f, 2.0f); Start而不是FixedUpdate InvokeRepeating以秒为单位调用方法methodName ,然后每隔repeatRate秒重复一次。 您可以在此处查看文档。

使用协程

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

然后可以使用StartCoroutine()启动它,并可以通过StartCoroutine()三种方式之一终止它:

  • 在内部通过带有break的while循环break (该函数将没有更多的行可以执行和退出)
  • 在内部通过yield break
  • 通过在对协程的引用上调用StopCoroutine()在外部进行

其他答案的替代方法:只需使用倒数计时即可。 有时这可以让您更好地控制

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

我通过考虑您的反馈来更新脚本,它的工作原理很吸引人。 谢谢大家!

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