Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Schytheron · Aug 27, 2015 at 11:55 AM · unity 5functionontriggerexitportal

OnTriggerExit2D is being ignored! (I think I need to retarget the function but not sure how)

Hi, Im trying to make a "Portal"- like 2D game and I have started making the portal scripts (it is very barebones right now) and In when I walked thru one of the portals it kept teleporting me back and forth between the two portals due to collision. So I decided to turn off the colliders of the player when entering one of the portals and trying to re-enable the colliders when exiting the portal triggers. It does disable the player colliders when entering the portal but it refuses to enable them again afterwards and I have no idea why!

 using UnityEngine;
 using System.Collections;
 
 public class PortalScript : MonoBehaviour {
     public GameObject otherportal;
     public GameObject player;
     private Collider2D otherportalCollider;
 
     void Awake(){
         otherportalCollider = otherportal.GetComponent<BoxCollider2D>();
 
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         if(other.tag == "Player"){
             other.GetComponent<BoxCollider2D>().enabled = false;
             other.GetComponent<CircleCollider2D>().enabled = false;
             other.transform.position = otherportal.transform.position;
         }
     }
 
     void OnTriggerExit2D(Collider2D other){
         if(other.tag == "Player"){
             other.GetComponent<BoxCollider2D>().enabled = true;
             other.GetComponent<CircleCollider2D>().enabled = true;
         }
 
     }
 
 
 }
 

Im suspecting that it is because the OnTriggerExit2D function is not checking the collider of the OTHER portal and is instead checking itself (script is attached to both portals). How do I make the on OnTriggerExit2D function check the other portal instead?

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

Answer by SuperRaed · Aug 27, 2015 at 02:16 PM

Is there any particular reason why you need to transform the player to the actual trigger and not just the portal? you realize that the trigger doesn't need to be the full size of the portal maybe just a line if crossed you will be transported somewhere close to it in the other portal?

Comment
Add comment · Show 3 · 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
avatar image Schytheron · Aug 27, 2015 at 06:18 PM 0
Share

@SuperRaed Wow I feel so dumb. Why did I never thing of resizing the trigger?!

avatar image Schytheron · Aug 27, 2015 at 06:26 PM 0
Share

@SuperRaed Nope it didnt work by resizing the trigger and I cant put an offset to the teleport location because it wouldn't look as if the character came out of the portal if I did. Would look as if he just teleported and not stepped thru a portal.

avatar image SuperRaed · Aug 27, 2015 at 08:40 PM 0
Share

How about using the modulus operator on a int lets call it portal_control where it's value is going to be either 0(turn portal On) or 1(turn portal Off). here's what I did and it's working for me

 void OnTriggerExit(Collider other){
 
         control_portal ++;
         control_portal = control_portal % 2;
 
         if ( control_portal == 0 ) {// portal switch is On teleport me to the other portal
             
             if(other.gameObject.name=="right"){
                 portal_index = 1;
             }
 
             else if( other.gameObject.name =="left"){
                 portal_index = 0;
             }
 
             transform.position = portals[portal_index].position;
         } else if (control_portal == 1) {//portal is Off i.e Just got teleported here from the other portal do nothing
             
         }
 
     } 

just make sure to initialize portal_control to -1 at the beginning;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

void OnTriggerExit 3d not working on my script? 0 Answers

When I used portal in My Game why Destroyed “Camera Fallow”? 0 Answers

Animation play in reverse OnTriggerExit 1 Answer

calculate what angle should the turret fire with a rigibody bullet [C#] 0 Answers

How can I make a collider that gives me stat up to stop giving me it? 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