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?
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?
@SuperRaed Wow I feel so dumb. Why did I never thing of resizing the trigger?!
@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.
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
Follow this Question
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