我试图让我的玩家在场景中点亮火把,但不太确定如何做到这一点。
我有一个带有粒子系统的手电筒预制件。每次玩家的 torch 与未点燃的 torch 相撞时,我都希望该 torch 开始燃烧。
我一直在努力遵循文档,但一直无法理解(https://docs.unity3d.com/ScriptReference/ParticleSystem.html,https://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 并使用 OnCollisionEnter
和 OnCollisionExit
而不是 OnTriggerEnter
和 OnTriggerExit
。
附加到播放器:
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/