c# - Unity3D:让玩家使用 ParticleSystem 和 OnCollisionEnter 点亮手电筒?

标签 c# unity3d

我试图让我的玩家在场景中点亮火把,但不太确定如何做到这一点。

我有一个带有粒子系统的手电筒预制件。每次玩家的 torch 与未点燃的 torch 相撞时,我都希望该 torch 开始燃烧。

我一直在努力遵循文档,但一直无法理解(https://docs.unity3d.com/ScriptReference/ParticleSystem.htmlhttps://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html)。

这里也有这个问题:https://answers.unity.com/questions/1491419/having-player-light-torches-using-particle-system.html

我当前的代码如下。我将每个 torch 对象标记为 torch ,并将我的玩家标记为玩家。所有粒子系统,除了玩家的手电筒,都关闭“Play on Awake”并预热。

有什么建议或提示吗?

谢谢!

/* 
 * Attach this script to all the torches. It will be used to start the fire 
   using OnCollision?/OnTrigger? See which is better
 * Start with the particle effect/light being off, get all the components
 * Turn the torches on when the player's torch collides with them
 * 1.) Must make sure each torch object has a collider
 * */

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

public class StartFire : MonoBehaviour
{
    public GameObject torch;
    public ParticleSystem fireParticleSystem;
    bool lightOn;

    void Start()
    {
        lightOn = false; //Start with the light off
        fireParticleSystem = GetComponent<ParticleSystem>(); //get Particle System 
        torch = GetComponent<GameObject>(); //get Torch
    }

   /*
    * if player's torch hits this torch (that is not lit)
    * Turn on the fire
    * Set the light being on to true
    * */
   private void OnCollisionEnter(Collision collision) 
   {
        if(this.gameObject.tag==("torch") && collision.gameObject.tag==("Player") && lightOn==false)
        {
           fireParticleSystem.Play(); //start the particle system
           lightOn = true;
        }
    }
 }

最佳答案

1.创建您的 ParticleSystem 并将其 GameObject 的标签更改为“torch”。

2.使用 ParticleSystem 将 BoxCollider 附加到 SphereCollider 到该游戏对象。

3。将 #2 创建的碰撞器的 IsTrigger 标记为 true,因为与 a 碰撞没有意义触碰。看起来您只想检测玩家何时触摸它。

4.触摸脚本应该附加到播放器而不是触摸。使用 OnTriggerEnter 处理检测并检测玩家何时触摸触摸灯,然后使用 GetComponent 获取 ParticleSystem 并播放它。在 OnTriggerExit 中停止粒子。

如果您确实希望玩家发生碰撞并被触摸停止,请忽略 #2 并使用 OnCollisionEnterOnCollisionExit 而不是 OnTriggerEnterOnTriggerExit

附加到播放器:

public class ParticlePlayer : MonoBehaviour
{

    void OnTriggerEnter(Collider other)
    {
        //Make sure player is touching a touch
        if (other.CompareTag("torch"))
        {
            //Get ParticleSystem from the Gameobject the player collided with
            ParticleSystem ps = other.GetComponent<ParticleSystem>();

            //Play Particle
            ps.Play();
        }
    }

    void OnTriggerExit(Collider other)
    {
        //Make sure player is touching a touch
        if (other.CompareTag("torch"))
        {
            //Get ParticleSystem from the Gameobject the player collided with
            ParticleSystem ps = other.GetComponent<ParticleSystem>();

            //Stop Particle
            ps.Stop();
        }
    }
}

关于c# - Unity3D:让玩家使用 ParticleSystem 和 OnCollisionEnter 点亮手电筒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49733313/

相关文章:

c# - 按值枚举索引

c# - 在 C# Unity 中获取实时视频流

android - Android IAP在测试过程中找不到项目

ios - iOS 9 上的 Facebook 7.0.3 登录问题

c# - 带有 Vuforia SDK 的 Unity3d 中 ARCamera 的 Raycast 问题

android - 从 Android 设备搜索时,Google Play 应用未显示在搜索结果中

c# - DEP0600 部署 UWP 项目时出错

c# - 使外键(字符串字段)可为空

c# - 使用 LINQ to SQL 进行动态查询

c# - 如何正确捕获来自 C#'s HttpClient getAsync so the error says more than "发生一个或多个错误的异常?