Search Unity

Having problems with teleport tiles after respawning.

Discussion in 'Scripting' started by CalvBore, Jan 26, 2015.

  1. CalvBore

    CalvBore

    Joined:
    Oct 23, 2014
    Posts:
    9
    So I've been working on small project on and off for a while now and I've run into a bit of a snag.

    I use teleportation tiles to move my player character from one area to another, for things like doors and cave entrances and whatnot. They all use this code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Teleport : MonoBehaviour
    5. {
    6.     public GameObject target;
    7.     public bool transported = true;
    8.  
    9.     void OnTriggerEnter2D (Collider2D other)
    10.     {
    11.         if (other.tag == "Player")
    12.         {
    13.  
    14.             if (transported == false)
    15.             {
    16.  
    17.                 transported = true;
    18.                 other.gameObject.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, 0);
    19.             }
    20.  
    21.         }
    22.     }
    23.  
    24.     void OnTriggerExit2D (Collider2D other)
    25.     {
    26.         if (other.tag == "Player")
    27.         {
    28.             transported = false;
    29.             target.gameObject.GetComponent<Teleport>().transported = true;
    30.         }
    31.     }
    32. }

    I just recently implemented a respawn system that is quite similar to my teleport tiles, The player will be teleported to the last respawn tile they stepped over when their health reaches zero. These tiles all use this script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Teleport : MonoBehaviour
    5. {
    6.     public GameObject target;
    7.     public bool transported = true;
    8.  
    9.     void OnTriggerEnter2D (Collider2D other)
    10.     {
    11.         if (other.tag == "Player")
    12.         {
    13.  
    14.             if (transported == false)
    15.             {
    16.  
    17.                 transported = true;
    18.                 other.gameObject.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, 0);
    19.             }
    20.  
    21.         }
    22.     }
    23.  
    24.     void OnTriggerExit2D (Collider2D other)
    25.     {
    26.         if (other.tag == "Player")
    27.         {
    28.             transported = false;
    29.             target.gameObject.GetComponent<Teleport>().transported = true;
    30.         }
    31.     }
    32. }
    My only problem here is that if my player's health reaches zero after they've gone through a normal teleporter and is sent to respawn before the normal teleporter tiles then the teleporter tile will not work the first time they step on it, but if the step off of the tile and then back on again it will work as it should. I would appreciate any help anybody can give me with this issue. Thanks.