[ユニティ]ユニティパーティクルトリガー



Unity Particle Trigger



水鉄砲が当たる場所など、プロジェクトではパーティクルの衝突やトリガーが使用されることがあります。ヒットする必要があるときにコールバックするメソッドがあります。




水鉄砲の粒子設定




ここで注意すべきことは、衝突とトリガーです。


水鉄砲の粒子に関するスクリプトがあります



ParticleXFBase

#region Gavin using System.Collections.Generic using UnityEngine #endregion /// /// Fire particles /// public class ParticleXFBase : MonoBehaviour { protected ParticleSystem PS private GameObject Foam private bool _isOpened //Whether the water valve is open public Vector3 StartPoint // public Vector3 Destination public float ShotSpeed public float LifeTime public GameObject XFTarget { get set } public string FireGunPoint { get set } public double PointValue { get set } private int _triggerThreshold = 30 private float _ffTimer //Fire timer protected float Interval = 1.0f //The interval between two sprays (default) protected bool IsSendPoint = false //Whether the fire monitor score has been sent List enter = new List() List exit = new List() List inside = new List() List outside = new List() public int TriggerThreshold { get { return _triggerThreshold } set { _triggerThreshold = value } } public void ToggleWater(bool isOpen) { IsSendPoint = false gameObject.SetActive(isOpen) } public virtual void Init() { transform.localPosition = Vector3.zero transform.localEulerAngles = Vector3.zero //****************Core code PS = GetComponent() if (XFTarget != null) PS.trigger.SetCollider(0, XFTarget.GetComponentInChildren(true)) //******************* var mainModule = PS.main { mainModule.startSpeed = ShotSpeed mainModule.startLifetime = LifeTime } } // protected virtual void OnParticleCollision(GameObject other)//Particle collision detected // { // if (_isOpened == false) return // // if (other.CompareTag(XFTarget))//Fire monitor particles need to increase the XFP Tag // { // if (IsSendPoint) return // Debug.LogFormat('{0}___ Particles are coming..', XFTarget) // _ffTimer += Time.deltaTime // if (_ffTimer >= Interval)//The collision time difference of continuously detected fire particles is less than fDeltaTime // { // _ffTimer -= Interval // DataCommModule.Instance.SetPointValue(FireGunPoint, PointValue, RoleManager.Instance.currentRoleID, 1) // Debug.LogFormat('{0}________ sent a point value..', XFTarget) // IsSendPoint = true // } // } // } private int debugCount = 0 protected virtual void OnParticleTrigger() { int enterNum = PS.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter) int exitNum = PS.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit) int insideNum = PS.GetTriggerParticles(ParticleSystemTriggerEventType.Inside, inside) int outsideNum = PS.GetTriggerParticles(ParticleSystemTriggerEventType.Outside, outside) if (enterNum > 0) OnEnterCollider(enter) if (exitNum > 0) OnExitCollider(exit) if (insideNum > 0) OnInSideCollider(inside) if (outsideNum > 0) OnOutSideCollider(outside) #if UNITY_EDITOR // if (debugCount = Interval) { _ffTimer -= Interval int id = RoleManager.Instance.currentRoleID DataCommModule.Instance.SetPointValue(FireGunPoint, PointValue, id, 1) IsSendPoint = true Debug.LogFormat('________ sent a point..{0}value{1}', FireGunPoint, PointValue) } } public virtual void OnEnterCollider(List enters) { } public virtual void OnExitCollider(List exits) { } public virtual void OnInSideCollider(List insides) { Debug.Log('Cc') if (insides.Count > _triggerThreshold) { FixedAction() } } public virtual void OnOutSideCollider(List outsides) { } }単に衝突に依存することは機能しません。コードで指定する必要があります。 PS = GetComponent() if (XFTarget != null) PS.trigger.SetCollider(0, XFTarget.GetComponentInChildren(true))ここでのXFTargetは、下の図のキューブコライダーです。



この場合、パーティクルがヒットすると、コールバックに入ります。

public virtual void OnInSideCollider(List insides)