Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dxung0 · Jun 11, 2021 at 07:57 PM · 2d gameunity 2dtriggersask

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 upalt text

image-2021-06-11-194619.png (152.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

overhead game in 2d world? 0 Answers

How to smooth out character Roll in a Top-down 2D movement 0 Answers

i have a problem with reloading with Time.time 2d game c# Unity 2020.2 1 Answer

Trigger in child - boxcollider2d without trigger on parent issues. 0 Answers

Way to achieve 3 lane mechanism in a 2D game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges