
どのドアを通ってどの部屋に出入りしたかを、トリガーを使って判定してみます。
まず簡単に部屋と出入り口を一つ作り、出入り口を挟むようにコライダーを2つ置きました。

コライダーはIs Triggerにチェックが入っていて、出口と入り口で別の名前のタグが付いています。

これらのトリガーに入ったことを調べます。
public bool isInMyRoom = true;
[SerializeField] Text text;
// ---
// Update
text.text = isInMyRoom + "";
// ---
// OnTriggerExit
private void OnTriggerExit(Collider other)
{
if (other.tag == "DoorEnter")
{
isInMyRoom = true;
}
else if (other.tag == "DoorExit")
{
isInMyRoom = false;
}
}
/*
private void OnTriggerEnter(Collider other)
{
if (other.tag == "DoorEnter")
{
isInMyRoom = true;
}
else if (other.tag == "DoorExit")
{
isInMyRoom = false;
}
}
*/
OnTriggerEnterを使うと、コライダーの隙間が狭いときに、途中で引き返すと問題が置きます。


なので、隙間を広くするかOnTriggerExitを使うといいと思います。OnTriggerExitの場合、コライダーの隙間が広いと少し外に出てもまだ部屋の中と判定されるので、コライダーが重なるぐらい近付けました。


さらに、コライダーをドアの子にして、ドアを部屋の子にすることで、同じタグを使って、どの部屋のどのドアから出入りしたのかがわかるようにしてみました。


// Update
//text.text = isInMyRoom + "";
// ---
// OnTriggerExit
private void OnTriggerExit(Collider other)
{
if (other.tag == "DoorEnter")
{
isInMyRoom = true;
Transform t = other.transform;
text.text = t.root.name + "に" + t.parent.name + "から入った";
}
else if (other.tag == "DoorExit")
{
isInMyRoom = false;
Transform t = other.transform;
text.text = t.root.name + "を" + t.parent.name + "から出た";
}
}
これだと、部屋に入るときに、先に「出た」と判定されるので修正がいるかもしれません。