I am trying to make a character teleport from Transform A to Transform B, and then if the player wants, to be able to exit B then walk back into it to be teleported back to A.
A two-way teleport.
The following code allows player to transport between the two points, but something is not working because it makes them teleport continuously back and forth between the two points. Something in the Stay and Exit logic is not right?:
public float WaitForSeconds = 5f;
public int spawnDelay = 2;
public Transform spawnpoint;
public Transform teleport, teleportExit;
public bool inTeleport = false;
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
// StartCoroutine (RespawnPlayer());
inTeleport = true;
transform.position = new Vector2 (teleportExit.position.x, teleportExit.position.y);
}
if (target.gameObject.tag == "teleportExit" && inTeleport == false) {
// StartCoroutine (RespawnPlayer());
transform.position = new Vector2 (teleport.position.x, teleport.position.y);
}
}
void OnTriggerStay2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
inTeleport = true;
}
}
void OnTriggerExit2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
inTeleport = false;
}
}
public IEnumerator RespawnPlayer () {
Debug.Log ("got to coroutine");
yield return new WaitForSeconds (5f);
}
Alright this seems to get the job done:
public float WaitForSeconds = 5f;
public int spawnDelay = 2;
public Transform spawnpoint;
public Transform teleport, teleportExit;
public bool inTeleport = false;
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "teleport" && inTeleport == false) {
transform.position = new Vector2 (teleportExit.position.x, teleportExit.position.y);
}
if (target.gameObject.tag == "teleportExit" && inTeleport == false) {
transform.position = new Vector2 (teleport.position.x, teleport.position.y);
}
}
void OnTriggerStay2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
inTeleport = true;
}
if (target.gameObject.tag == "teleport") {
inTeleport = true;
}
}
void OnTriggerExit2D(Collider2D target){
if (target.gameObject.tag == "teleportExit") {
inTeleport = false;
}
if (target.gameObject.tag == "teleport") {
inTeleport = false;
}
}
Try this and say if you got any problems with using this or using array for adding more locations
// You can set as many teleport locations using this . For that you can just use array.
//Set your your locations here
public Transform spawnPoint1 ;
public Transform spawnPoint2 ;
//Custom delay for telportation.The less the fast
public float addCustomeTeleportationTime ;
private bool canTeleport ;
private float _time ;
void Start()
{
_time = Time.time ;
}
void OnTriggerEnter(Collider2D col)
{
if ((col.tag == "teleport") && Time.time > _time + addCustomeTeleportationTime )
{
Teleport(col);
_time = Time.time ;
}
}
void Teleport(Collider2D colData)
{
if(colData.name == spawnPoint1.name) // you can actually use any condition to check ,.. as per your requirement
{
transform.position = spawnPoint2.position ;
}
else if(colData.name == spawnPoint2.name)
{
transform.position = spawnPoint1.position ;
}
}