- Home /
Having problems with my teleport tiles and respawning
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:
using UnityEngine;
using System.Collections;
public class Teleport : MonoBehaviour
{
public GameObject target;
public bool transported = true;
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
{
if (transported == false)
{
transported = true;
other.gameObject.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, 0);
}
}
}
void OnTriggerExit2D (Collider2D other)
{
if (other.tag == "Player")
{
transported = false;
target.gameObject.GetComponent<Teleport>().transported = true;
}
}
}
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:
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour
{
Teleport teleport;
HealthScript healthscript;
void Start ()
{
healthscript = GameObject.Find("PlayerCrimson").GetComponent<HealthScript> ();
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
{
healthscript.respawn = this.gameObject;
}
}
}
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.
Answer by CalvBore · Jan 30, 2015 at 10:25 AM
I figured it out! I just had to manipulate the transported bool.
At first i tried it only in my respawn script. This is what I came up with:
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour
{
Teleport teleport;
HealthScript healthscript;
void Start ()
{
healthscript = GameObject.Find("PlayerCrimson").GetComponent<HealthScript> ();
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
{
healthscript.respawn = this.gameObject;
GameObject.FindWithTag ("Transport").GetComponent<Teleport>().transported = false;
}
}
}
But, after i tried this i found that the teleportation tile on the opposite side wouldn't work correctly. So, i modified the script for my teleport tiles to thie:
using UnityEngine;
using System.Collections;
public class Teleport : MonoBehaviour
{
public GameObject target;
public bool transported = true;
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
{
if (transported == false)
{
if(target.gameObject.GetComponent<Teleport>().transported == false)
{
target.gameObject.GetComponent<Teleport>().transported = true;
}
transported = true;
other.gameObject.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, 0);
}
}
}
void OnTriggerExit2D (Collider2D other)
{
if (other.tag == "Player")
{
transported = false;
target.gameObject.GetComponent<Teleport>().transported = true;
}
}
}
Now everything works the way I would like it to. Easier fix than i thought it would be.
Your answer
Follow this Question
Related Questions
2D - Creating a basic teleport system 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Teleport Not Working in Unity 2D 1 Answer