facebook twitter hatena line email

「Unity/UniRx/接触判定/侵食2D」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「<pre> using UnityEngine; using UniRx; using UniRx.Triggers; using System; public class ColliderTrigger2dScene : MonoBehaviour { [SerializeField] ObservableTrigge...」)
 
 
行1: 行1:
 +
==接触じゃなくて通り抜けの場合==
 +
 
<pre>
 
<pre>
 
using UnityEngine;
 
using UnityEngine;

2023年3月6日 (月) 19:40時点における最新版

接触じゃなくて通り抜けの場合

using UnityEngine;
using UniRx;
using UniRx.Triggers;
using System;

public class ColliderTrigger2dScene : MonoBehaviour
{
    [SerializeField]
    ObservableTrigger2DTrigger landTrigger;
    void Start()
    {
        IObservable<Collider2D> landOnTriggerEnterAsObservable = landTrigger.OnTriggerEnter2DAsObservable();
        IObservable<Collider2D> landOnTriggerStayAsObservable = landTrigger.OnTriggerStay2DAsObservable();
        IObservable<Collider2D> landOnTriggerExitAsObservable = landTrigger.OnTriggerExit2DAsObservable();

        landOnTriggerEnterAsObservable.Subscribe(collider =>
        {
            Debug.Log("OnTriggerEnter2D collider.gameObject.name=" + collider.gameObject.name); // 接触object名
        });
        landOnTriggerStayAsObservable.Subscribe(collider =>
        {
            Debug.Log("OnTriggerStay2D collider.gameObject.name=" + collider.gameObject.name); // 接触object名
        });
        landOnTriggerExitAsObservable.Subscribe(collider =>
        {
            Debug.Log("OnTriggerExit2D collider.gameObject.name=" + collider.gameObject.name); // 接触object名
        });
    }
}