- Home /
Weird problem with triggers
I'm having a wierd problem with my triggers. I creat a room with a polygon collider Trigger for cinemachine and in that room, i also put a chest with a box collider Trigger. Everything run just fine until i get in the zone of chest trigger. After I leave this trigger, both room and chest's trigger use OnTriggerExit2D. Is there anyone facing this problem too ?
p/s : I created a layer for room and chest seperately and didnt make it collider with each other but it seems not to be the solution p/s : Here is my code For the room :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Room : MonoBehaviour
{
[SerializeField] protected Enemy[] _enemies;
[SerializeField] protected Pot[] _pots;
[SerializeField] protected GameObject _virtualCamera;
public virtual void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") && !other.isTrigger)
{
for (int i =0; i < _enemies.Length; i++)
{
ChangeActivation(_enemies[i], true);
}
for (int i = 0; i < _pots.Length; i++)
{
ChangeActivation(_pots[i], true);
}
_virtualCamera.SetActive(true);
}
}
public virtual void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player") && !other.isTrigger)
{
for (int i = 0; i < _enemies.Length; i++)
{
ChangeActivation(_enemies[i], false);
}
for (int i = 0; i < _pots.Length; i++)
{
ChangeActivation(_pots[i], false);
}
_virtualCamera.SetActive(false);
}
}
public void OnDisable()
{
_virtualCamera.SetActive(false);
}
protected void ChangeActivation(Component component, bool activation)
{
component.gameObject.SetActive(activation);
}
}
For the chest:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TreasureChest : Interactable
{
[Header("Chest Stats")]
[SerializeField] private Item _content;
[SerializeField] private bool _isOpen;
[SerializeField] private BoolValue _StoredOpenedStat;
[Header("Effects")]
[SerializeField] private GameObject _dialogWindow;
[SerializeField] private Text _dialogText;
[SerializeField] private Inventory _invent;
private Animator _chestAnim;
[Header("signal")]
[SerializeField] private SignalScript _raiseItem;
// Start is called before the first frame update
void Start()
{
_chestAnim = GetComponent<Animator>();
_isOpen = _StoredOpenedStat._runtimeValue;
if (_isOpen)
{
_chestAnim.SetBool("alreadyOpened", true);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("attack") && _playerInRange)
{
if (!_isOpen)
{
//open chest
OpenChest();
}
else
{
//Chest remains opening.
CloseChest();
}
}
}
public void OpenChest()
{
//dialog Window On
_dialogWindow.SetActive(true);
//dialog Text = contents Text
_dialogText.text = _content._itemDescription;
//add content to inventory
_invent.AddItem(_content);
_invent._currentItem = _content;
//Raise signal to animate the chest
_raiseItem.Raise();
//raise context clue off
_contextOff.Raise();
//set the chest to opened
_isOpen = true;
_chestAnim.SetBool("opened", true);
_StoredOpenedStat._runtimeValue = _isOpen;
}
public void CloseChest()
{
_playerInRange = false;
//dialog Off
_dialogWindow.SetActive(false);
//Raise signal to stop animation
_raiseItem.Raise();
}
protected override void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") && !other.isTrigger && !_isOpen)
{
_contextOn.Raise();
_playerInRange = true;
}
}
protected override void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player") && !other.isTrigger && !_isOpen)
{
_contextOff.Raise();
_playerInRange = false;
}
}
}
`
here is my unity set up
image-2021-06-11-194619.png
(152.7 kB)
Comment