【问题标题】:Put Unity Particle System in Apple Location将 Unity 粒子系统放入 Apple 位置
【发布时间】:2020-06-26 02:53:24
【问题描述】:

我对 Unity 很陌生,我正在制作一个蛇游戏。我有一个功能可以在你吃苹果时激活粒子系统。但是,粒子系统位于屏幕中间,但我希望它位于苹果所在的位置。这是我的苹果产卵位置代码

void RandomlyPlacedApple()
    {
        int ran = Random.Range(0, availbleNodes.Count);
        while (isTailNode(availbleNodes[ran]))
        {
            ran = Random.Range(0, availbleNodes.Count);
        }
        PlacePlayerObject(appleObj, availbleNodes[ran].worldPosition);
        appleNode = availbleNodes[ran];
    }

谢谢

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

    最简单的事情是,如果您只是将您的粒子系统父级添加到一个空的游戏对象,我们将其称为 appleParticles,然后在苹果的位置实例化该游戏对象。

    例如,这些行应该在您的播放器脚本中,并且应该在您与苹果碰撞时发生。为此,您的玩家需要一个 2D BoxCollider,以及需要 2D Box Collider 的苹果,您需要将他的 Tag 设置为“Apple”。

    添加之后,您现在可以将此代码添加到您的播放器脚本中:

      GameObject appleParticles;
    
      void OnCollisionEnter2D(Collision col)
        {
            //Check if the object isn't an Apple
            if (!col.CompareTag("Apple"))
               return;
           
           // You spawn the particles at the position of the apple
           Instantiate(appleParticles , apple);
           
           // Starting the particle Effects (You can also let it start automatically when you spawn them in)
           appleParticles.GetComponentInChildren(ParticleSystem).Play();
    
           // After that destroy the apple, because you ate it
           Destroy(col.gameObject);
    
           // If you want to destroy the particle system after a certain time as well you can just call
           Destroy(appleParticles, 1f); // 1f means 1 second until it's destroyed
        }
    

    【讨论】:

    • 我把所有这些放在哪里?我应该把它放在 RandomlyPlacedApple 还是 Start 中?另外,当您说向 GameObject 添加粒子系统时,您的意思是像父对象一样吗?
    • 我更改了答案以包含您的所有问题,请立即检查是否有帮助。
    • 我想我开始明白了,但是在我尝试将我的苹果放在那里但它说无法将 SA.Node 转换为 UnityEngine.transform 的情况下。同样在 GetComponentInChildren 中,ParticleSystem 应该是我命名的 Empty?
    • 对于您的问题,您不需要添加对苹果的引用,您唯一要做的就是销毁游戏对象(并实例化粒子),只要碰撞游戏对象的标签是“苹果”。对于名称,您不需要/不应该这样做,因为您没有获得名称为 ParticleSystem 的对象。您真正要做的是在 appleParticles 游戏对象中搜索具有 ParticleSystem 类型的组件,然后得到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多