简体   繁体   English

如何在 Unity 中重生对象?

[英]How can I respawn an object in Unity?

I am working on a 3d basketball game in unity.我正在统一制作 3d 篮球游戏。

It has a score and a timer.它有一个分数和一个计时器。 After a certain time my scene loads and starts from the beginning.一段时间后,我的场景加载并从头开始。 Every time I throw the ball, I have to spawn it.每次我扔球时,我都必须生成它。

It has a spawn button and a shoot button.它有一个生成按钮和一个射击按钮。 But I don't want to use the spawn button.但我不想使用生成按钮。 So I want to spawn the ball automatically.所以我想自动生成球。

How should I do it?我该怎么做? I am giving my spawn button code and throw button code bellow.我给出了我的生成按钮代码和下面的抛出按钮代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnButton: MonoBehaviour
{

    public GameObject ball;
    public GameObject BallPosition;

    public void Spawn()
    {
        ball.transform.position = BallPosition.transform.position;
        var ballPosition = ball.transform.position;
        ball.GetComponent<Rigidbody>().useGravity = false;
        ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
        ball.transform.position = ballPosition;
    }

}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThrowButton: MonoBehaviour
{
    static Animator anim;
    public GameObject ball;
    public float ballThrowingForce = 5f;
    internal bool holdingBall;


    void Start()
    {
        anim = GetComponent<Animator>();
        ball.GetComponent<Rigidbody>().useGravity = false;
    }


    public void Throw()
    {
        anim.SetTrigger("isThrowing");
        StartCoroutine(Test());        
    }

    IEnumerator Test()
    {
        yield return new WaitForSeconds(1.5f);
        ball.GetComponent<Rigidbody>().useGravity = true;
        //ball.GetComponent<Rigidbody>().AddForce(transform.up * ballThrowingForce);
        ball.GetComponent<Rigidbody>().AddForce(0, 380.0f, ballThrowingForce);       
    }

}

To spawn a ball you should create a prefab and instantiate it.要生成一个球,您应该创建一个预制件并实例化它。 Example:例子:

private class Spawner : MonoBehaviour
{
    public GameObject prefab;

    public GameObject Spawn() => Instantiate(prefab);
}

So that your throw code should spawn a ball and if you want destroy an older one.这样你的投掷代码应该产生一个球,如果你想摧毁一个旧的。

The above answer by Iv Misticos mentioned the use of Instantiate which is a great way to spawn a new ball. Iv Misticos 的上述回答提到了使用 Instantiate,这是生成新球的好方法。 before you spawn a new ball, you need to specify when it must spawn.在生成新球之前,您需要指定它何时必须生成。

Either you can要么你可以

  1. use a Invoke("SpawnNewBallMethod",3) //after this piece of code is executed, it waits for 3 seconds and executes the method you mention in it which can have the code to instantiate. use a Invoke("SpawnNewBallMethod",3) //这段代码执行完后,等待3秒,执行你在里面提到的可以实例化代码的方法。

  2. Or After the first ball's work is done, you can destroy it(Destroy(ball)) or set active to false - Then check the state (if it exists(null check?) or gameObject.active==true) and accordingly instantiate a new ball (if it is setactive(false) don't forget to destroy it later).或者在第一个球的工作完成后,您可以销毁它(Destroy(ball)) 或将 active 设置为 false - 然后检查状态(如果它存在(空检查?)或 gameObject.active==true)并相应地实例化一个新球(如果它是 setactive(false) 不要忘记稍后销毁它)。

Although gameObject.active is obsolete, it will serve the purpose without complicating things.尽管 gameObject.active 已过时,但它不会使事情复杂化。 In my opinion, Invoke is better than a IEnumerator here as Invoke will not stop the execution flow and continue with the other things while the timer is running.在我看来,Invoke 在这里比 IEnumerator 更好,因为 Invoke 不会停止执行流程并在计时器运行时继续其他事情。

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

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