Last active
August 29, 2022 10:45
-
-
Save Tunied/8e9f98f15f9ffd362864497b1aff84bd to your computer and use it in GitHub Desktop.
CompoundTrigger for demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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