销毁游戏对象并实例化另一个游戏对象

问题描述 投票:0回答:1

我有在与地形碰撞时销毁Cube GameObject的代码。但是,我不确定在销毁多维数据集之后将New Sphere GameObject实例化后如何处理。

这是当前代码:

{
void OnCollisionEnter(Collision collision)
{
    if (collision.collider.gameObject.tag != "Destroy") 
    {
        Destroy (gameObject);
    }
}

}

unity3d instantiation gameobject
1个回答
0
投票

1)将此脚本附加到地形游戏对象而不是立方体。

2)在编辑器中为多维数据集对象(例如多维数据集)添加一个新的tag

3)创建一个新的sphere prefab实例,您可以通过包含OnCollisionEnter()事件的脚本进行访问。

        void OnCollisionEnter(Collision collision)
        {
           if (collision.collider.gameObject.tag == "Cube")
           {
             //store the transform component of the gameobject to be destroyed.
             var transf = collision.gameObject.transform;

             //Destroy the collided gameobject
             DestroyImmediate(gameObject);

             //Instantiate in the position and rotation of the destroyed object.
             Instantiate(sphere, transf.position, transf.rotation);
           }
        }
© www.soinside.com 2019 - 2024. All rights reserved.