Skip to content

Instantly share code, notes, and snippets.

@Tunied
Last active August 29, 2022 10:45
Show Gist options
  • Save Tunied/8e9f98f15f9ffd362864497b1aff84bd to your computer and use it in GitHub Desktop.
Save Tunied/8e9f98f15f9ffd362864497b1aff84bd to your computer and use it in GitHub Desktop.
CompoundTrigger for demonstration
using System.Collections.Generic;
using UnityEngine;
public class Mono_CompoundTrigger : MonoBehaviour
{
public class ColliderInfo
{
public Collider target;
public int count;
}
private readonly List<ColliderInfo> mAllNowStayTargetList = new();
private void FixedUpdate()
{
for (var i = mAllNowStayTargetList.Count - 1; i >= 0; i--)
{
var info = mAllNowStayTargetList[i];
info.count--;
if (info.count < 0)
{
mAllNowStayTargetList.Remove(info);
Debug.Log($"{info.target} <color=red>OnTriggerOut</color>");
}
}
}
private void OnTriggerStay(Collider other)
{
var info = mAllNowStayTargetList.Find(x => x.target == other);
if (info != null)
{
//Log -> TargetStay
info.count = 1;
}
else
{
var newInfo = new ColliderInfo();
newInfo.count = 1;
newInfo.target = other;
mAllNowStayTargetList.Add(newInfo);
Debug.Log($"{other} <color=green>OnTriggerEnter</color>");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment