简体   繁体   English

销毁后如何重生对象?

[英]How to respawn objects after destroyed?

I am very new on developing games.我对开发游戏很陌生。 I decided to make a game simply with a player and 5 different objects around it.我决定制作一个简单的游戏,其中包含一个玩家和 5 个不同的物体。 Any time player collides with the one of the object, then all objects destroyed and respawn again.任何时候玩家与 object 中的一个发生碰撞,然后所有对象都会被摧毁并再次重生。 So far i managed to destroy them but couldt respawn them.到目前为止,我设法摧毁了它们,但无法重生它们。

Could you please help me about it, Thank you:)你能帮我解决一下吗,谢谢:)

here is the code这是代码

public class collectibles : MonoBehaviour
{
         public GameObject[] spawncollectable;
         bool isobjdestroyed=false;
    void Start()
    {
        float radians = 2 * Mathf.PI * 1.3f;
        float vert = Mathf.Sin(radians);
        float horiz = Mathf.Cos(radians);
        var spawnDir = new Vector3(horiz + Random.Range(-0.1f, 1.3f), 0.2f, vert + Random.Range(-0.1f, -1.3f));
        var spawnPos = transform.position + spawnDir;
        Instantiate(spawncollectable[Random.Range(0, spawncollectable.Length)],spawnPos, Quaternion.identity);
    }
    private void Update()
    {
        if (isobjdestroyed == true)
        {
            float radians = 2 * Mathf.PI * 1.3f;
            float vert = Mathf.Sin(radians);
            float horiz = Mathf.Cos(radians);
            var spawnDir = new Vector3(horiz + Random.Range(-0.1f, 1.3f), 0.2f, vert + Random.Range(-0.1f, -1.3f));
            var spawnPos = transform.position + spawnDir;
            Instantiate(spawncollectable[Random.Range(0, spawncollectable.Length)], spawnPos, Quaternion.identity);
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            GameObject[] obje = GameObject.FindGameObjectsWithTag("obj");
            foreach (GameObject o in obje)
            {
                GameObject.Destroy(o);
                isobjdestroyed = true;
            }
           
        }
    }
}

A solution: Rather than use GameObject.Destroy(), Use.SetActive(false) and.SetActive(true) when the object reaches destruction or any other state where you need it to change the objects state. Destroy is meant to remove the component from the scene completely (Great for bullets for example where you are instancing the object over and over and over).解决方案:当 object 或任何其他 state 达到破坏时,而不是使用 GameObject.Destroy()、Use.SetActive(false) 和 .SetActive(true),您需要它来更改对象 state。Destroy 是为了删除组件完全来自场景(非常适合子弹,例如你一遍又一遍地实例化 object 的地方)。 but you don't need to do that if you are respawning the object in a basic way.但如果您以基本方式重生 object,则不需要这样做。 Just reset the the objects parameters such as health based on your requirements such as只需根据您的要求重置对象参数,例如健康状况,例如

if (health <= 0) // dont forget to reset your variables when you turn your object back on CharacterName.SetActive(fasle); if (health <= 0) // 当您将 object 重新设置为 CharacterName.SetActive(fasle) 时,不要忘记重置您的变量;

else CharacterName.SetActive(true);否则 CharacterName.SetActive(true);

There should be more elements to this but that is the general idea.应该有更多的元素,但这是一般的想法。 you will need to add a respawn function, where it resets position, it will need to then reset its health by reassigning its value, and it will need (most likely) a call to your animation trigger.您需要添加一个重生 function,它会重置 position,然后需要通过重新分配其值来重置其健康状况,并且(很可能)需要调用您的 animation 触发器。 hope that helps.希望有所帮助。 (this is how i would approach that problem. Though a basic explanation it should allow you to figure out your problem pretty quickly) (这就是我解决这个问题的方法。虽然是一个基本的解释,但它应该能让你很快找出你的问题)

Also with your var variables that pertain to the vector 3, they are always going to be floats, doubles or greater.此外,对于与向量 3 相关的 var 变量,它们始终是浮点数、双精度数或更大。 3d space is vast and can be a big numbers some times requiring change from float to doubles or long doubles. 3d 空间很大,有时可能是一个很大的数字,需要从 float 更改为 double 或 long double。 But they should be declared as such rather than using "var" as the type.但它们应该这样声明,而不是使用“var”作为类型。 run a test each area in your game from edge to edge to determine the max distance you can travel and use the correct variable type.从边到边对游戏中的每个区域进行测试,以确定您可以行进的最大距离并使用正确的变量类型。 this will work out better long term when working on bigger projects.在从事更大的项目时,这将长期有效。 var should only be used when you dont know what the value will be but the areas in a game can be determined and set correctly with relative ease.仅当您不知道该值是什么但可以相对轻松地正确确定和设置游戏中的区域时,才应使用 var。

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

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