I have an issue here that I just can’t understand.
I’ve a 2D game with two teleporters. A item enters one portal, enters the trigger area and then appears at the other portal. I’ve done this with a 3D game that conceptually works in the same way (it was 3D, but everything worked on the same plane, so the game area was effectively 2D) and it works perfectly, and code-wise, the only changes are mainly just changing colliders and triggers to 2D versions, and a few other small changes.
Each teleporter has a center gameobject, which is the target for the other (ie, Teleporter_A has Center_A, which teleports anything entering it to Center_B of Teleporter_B, and vice versa)
The problem is that only one teleporter works, no matter how many times I either manually make them or duplicate them and change over the targets (ie, Center_A/Center_B).
Eg, if I thrown something into Teleporter_A, it teleports to Teleporter_B perfectly. But if I throw something into Teleporter_B, it teleports to Teleporter_A for a split second and then immediately teleports back to B.
After adding a few print statements, I can confirm that for one teleporter, it seems to call the teleporting code twice. I just have no idea why. If the both didn’t work, then I could troubleshoot that. But when only one ever works, I can’t understand why.
I’ve both teleporters set up in the same way in everything apart from the targets in the code, so I’m guessing it must be a coding issue. If someone could please scan through it, maybe you could find an issue I’m blind to.
public Transform target; // drag the center object of another teleporter here in the inspector
public GameObject teleportEffect;
public Transform teleportCenter;
private MultiDragRigidbody2D dragR;
private GameObject teleportEffectInstance;
void Awake () {
dragR = GameObject.FindGameObjectWithTag ("LevelMGR").GetComponent<MultiDragRigidbody2D>() as MultiDragRigidbody2D;
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "Draggable") {
DraggableItem dItem = col.gameObject.GetComponent<DraggableItem>();
if (!dItem.isTeleporting){ // if it's not teleporting, then teleport it
if (dragR.shrinking){ // so the transform changes don't overlap and look weird.
return;
}
// Teleport the item
dItem.isTeleporting = true;
print ("Teleporting");
col.gameObject.transform.position = target.transform.position;
if (col.gameObject == dragR.dragGameObject){
dragR.DropDraggedItem(); // so if you drag an object into a teleport, it doesn't try and jump back towards your hand
}
// Play Sounds and effects
LevelMgr lvlMgr = GameObject.FindGameObjectWithTag("LevelMGR").GetComponent<LevelMgr>();
lvlMgr.PlaySoundEffect (SoundTypes.Teleporter, false);
if (teleportEffectInstance == null){
teleportEffectInstance = Instantiate (teleportEffect, teleportCenter.position, Quaternion.identity) as GameObject;
}
Destroy (teleportEffectInstance, 0.5f);
}
else { // if it is teleporting, they it must have just teleported here
if (teleportEffectInstance == null){
teleportEffectInstance = Instantiate (teleportEffect, teleportCenter.position, Quaternion.identity) as GameObject;
}
Destroy (teleportEffectInstance, 0.5f);
}
}
}
void OnTriggerExit2D (Collider2D col) {
if (col.gameObject.tag == "Draggable") {
DraggableItem dItem = col.gameObject.GetComponent<DraggableItem>();
if (dItem.isTeleporting){
dItem.isTeleporting = false;
}
}
}