I made a 2Dgame which has many rooms in one scene. Each room has a cinemachine virtual cam confined with the polygon collider bounding the room. My room has a script to turn on virtual cam when player go in room trigger zone and turn off when player leave. In my room, i have a chest with a trigger and a script to open the chest when player get in trigger zone and press “space”.
The problem is: after my player get in the trigger zone of chest, the virtual cam turn off (it stop following the player) even my player still in the room, and only turn on again if i go into trigger zone of chest. I checked the bounding shape of cam confiner while game running but it’s still room collider. I was new to unity so cannot find out what the problem is. Can someone help me?
Here’s the codes of my room and chest:
*Room
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//observer objects:
//we cannot transfer signals to active an inactive objects;
//so we need an object that always active to set other objects active
public class Room : MonoBehaviour
{
[SerializeField] protected Enemy[] _enemies;
[SerializeField] protected Pot[] _pots;
[SerializeField] protected GameObject _virtualCamera;
public bool _inRange;
public virtual void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") && !other.isTrigger)
{
_inRange = true;
//activate all enemies and pots
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)
{
_inRange = false;
//khi thanh phan va cham khong phai la trigger ma chi la collider
if (other.CompareTag("Player") && !other.isTrigger)
{
//deactivate all enemies and pots
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);
}
//lí do dùng component ở đây để không cần quan tâm đến component nào của gameObject sẽ được thêm vào
//với bất kỳ component nào được thêm vào thì ta sẽ thực hiện SetAcive với gameObject mà component đó liên kết
protected void ChangeActivation(Component component, bool activation)
{
component.gameObject.SetActive(activation);
}
}
*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;
}
}
}
And here’s the set up for my gameobjects: