【问题标题】:Unity3D - unable to respawn an object after it has been destroyedUnity3D - 对象被破坏后无法重生
【发布时间】:2014-09-28 07:10:16
【问题描述】:

我在一个预制件被破坏后重新生成它时遇到了问题。在被摧毁一秒钟后,我似乎无法让它在原来的起始位置重生。我创建了一个空游戏对象并将 SpawnTargets.cs 脚本附加到它。我不确定解决这种情况的最佳方法是什么。另一个附加了脚本的对象会实际销毁预制件。 BulletCollisionHandler.cs 工作正常。谢谢你的帮助。代码如下:

SpawnTargets.cs:

using UnityEngine;
using System.Collections;

public class SpawnTargets : MonoBehaviour 
{
    public GameObject targetCircle;
    public GameObject targetSquare;
    public GameObject targetStar;

    private Vector3 circleSpawnPosition = new Vector3(0.0f, 1.227389f, -7.5f);
    private Vector3 squareSpawnPosition = new Vector3(0.0f, 1.027975f, -7.993299f);
    private Vector3 starSpawnPosition = new Vector3(0.0f, 1.8f, -7f);

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
        SpawnTarget ();
    }

    void SpawnTarget()
    {

    }
}

BulletCollisionHandler.cs:

using UnityEngine;
using System.Collections;

public class BulletCollisionHandler : MonoBehaviour 
{
    public GameObject targetCircle;

    // Use this for initialization
    void Start () 
    {
        Destroy (gameObject, 2);
    }

    // Update is called once per frame
    void Update ()
    {

    }

    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.name == "TargetSquare")
        {
            other.gameObject.rigidbody.isKinematic = false;
            ((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false;

            Destroy (other.gameObject, 1);
            Debug.Log("Hit square");
        }
        else if(other.gameObject.name == "TargetCircle")
        {
            other.gameObject.rigidbody.isKinematic = false;
            ((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false;

            Destroy (other.gameObject, 1);

            Debug.Log("Hit circle");
        }
        else if(other.gameObject.name == "TargetStar")
        {
            other.gameObject.rigidbody.isKinematic = false;
            ((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false;
            ((TargetMovementVertical)other.gameObject.GetComponent<TargetMovementVertical>()).enabled = false;

            Destroy (other.gameObject, 1);
            Debug.Log("Hit star");
        }
    }
}

【问题讨论】:

  • SpawnTarget()的内容是什么?

标签: c# unity3d


【解决方案1】:

您没有在任何地方调用 Instantiate(),因此很难在您提供的代码中看到新对象的来源。

无论如何,最好不要使用 Destroy。如果你想立即重置对象,为什么不简单地将它回收到起始位置呢?避免实例化和销毁大量对象是个好主意,最好隐藏/禁用不需要的对象并取消隐藏/重新启用它们。

这里是a tutorial on the general idea. 该教程是关于对象组的,但同样的技巧也适用于回收单个对象。

【讨论】:

    【解决方案2】:

    你最好使用 gameObject.SetActive(true/false);用于激活/停用游戏对象,而不仅仅是使用 Destroy。

    然后,如果您使用 Destroy,您有 3 个选项可以在玩家看到之前将其置于所需位置。

    1) 在禁用其渲染器组件后启用游戏对象。然后你将变换的位置/旋转均衡到你需要的位置。在此之后,您重新启用渲染器组件。它应该放在你想要的地方。

    2) 您实例化游戏对象,但首先要确保其 Prefab 上的 Renderer 组件默认禁用,因此您可以重新分配其 Transform 值,然后 - 再次重新启用 Renderer。

    3) 你制作了一个不可见的游戏对象(一个空游戏对象)并实例化想要的游戏对象,然后你将 Empty 设置为新创建的游戏对象的父级。只要父级 Empty 正是你想要的位置,当您实例化并重置子级的位置时,它应该会跳到 Empty 父级的正上方。

    我没有提供代码,因为您没有提供代码,而且我不知道您最终会更喜欢哪种方法。就性能而言,启用/禁用是最佳选择。

    正如 theodox 所说,对象池是您最好的朋友,例如子弹,尽管它可能应用于许多其他游戏对象,这些游戏对象可能在您的游戏逻辑中作为“对象集合”工作。完全值得学习。

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 2013-05-21
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多