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 SirDrunkAlot · Apr 09, 2015 at 04:08 AM · 2dgameobjectcollider

Detecting collision switch while maintaining game functionality

Hi everyone, I have created a random generator for a prefab called "Tunnel". Whenever i collide with a tunnel prefab, i wish to keep the game running. If i am no longer colliding with a tunnel i wish the game to stop.

The game works fine for the first tunnel, but when i reach the end of the first tunnel (start of the second - they overlap), my "OnTriggerExit2D" function stops the game.

Is there a way to tell my "OnTriggerExit2D" to check if i'm colliding with a different tunnel?

Here's my code:

     void OnTriggerEnter2D(Collider2D other)
         {
             Debug.Log (other.gameObject.tag);
             if (other.gameObject.CompareTag ("LeftTunnel")) {
                 touchRef.onTunnelL = true;
             }
             if (other.gameObject.CompareTag ("RightTunnel")) {
 
                 touchRef.onTunnelR = true;
             } 
     
         }
     
         void OnTriggerExit2D(Collider2D other)
         {
             if(other.gameObject.CompareTag("LeftTunnel"))
             {
                 touchRef.OnTriggerExit2Dchild();
                 touchRef.onTunnelL = false;
             }
     
             if(other.gameObject.CompareTag("RightTunnel"))
             {
                 touchRef.OnTriggerExit2Dchild();
                 touchRef.onTunnelR = false;
             }
         } 
     
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SirDrunkAlot · Apr 09, 2015 at 07:55 PM

so, after consulting with some people i decided to change the hole mechanism to work with ray casting. it works gr8 now. here's the code for any1 in the future(i call cast function on Update):

 void Cast()                                                                                                                                        
     {
         for (int i = 0; i < Input.touchCount; ++i)
         {
             Vector2 test = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
             RaycastHit2D hit = Physics2D.Raycast(test, (Input.GetTouch(i).position));
             Vector2 touchPos = new Vector2(test.x, test.y);
             onLeft(touchPos);
             onRight(touchPos);
 
         }
     }
     void onLeft(Vector2 touchPos){
         Collider2D tempL;
         if (tempL = Physics2D.OverlapPoint (touchPos)) {
             if(tempL.CompareTag("LeftTunnel")){
             Debug.Log ("Hit Left tunnel");
             onTunnelL = true;
             }
         }
         else{
             onTunnelL = false;
         }
     }
     void onRight(Vector2 touchPos){
         Collider2D tempR;
         if (tempR = Physics2D.OverlapPoint (touchPos)) {
             if(tempR.CompareTag("RightTunnel")){
                 Debug.Log ("Hit Right tunnel");
                 onTunnelR = true;
             }
         }
         else{
             onTunnelR = false;
         }
     }
Comment
Add comment · Share
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Unity 2D Colliders not Colliding 2 Answers

OnCollisionEnter2D not working? 2 Answers

OnCollisionEnter2D not calling properly 2 Answers

Rotate a gameobject every time it collides with an “a” tag collider until the player does not press any key 2 Answers

How to achieve this ? ( pivoting) ! (2D sprite) 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