- Home /
Infinite portal loop!
i am having a problem with my portal game.when im teleporting to the second portal it teleports me back to the first one, creating an infinite loop
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class teleport_player : MonoBehaviour
{
public Transform teleportTarget;
private Transform player;
public GameObject thePlayer;
public GameObject theSelf;
private Transform self;
bool canteleport;
void OnTriggerEnter(Collider other)
{
canteleport = true;
if (canteleport)
{
canteleport = false;
player = thePlayer.transform;
self = theSelf.transform;
thePlayer.transform.position = teleportTarget.transform.position;
if (teleportTarget.rotation.y == self.rotation.y )
thePlayer.transform.rotation = Quaternion.Euler(player.rotation.eulerAngles.x, teleportTarget.rotation.eulerAngles.y - 180, player.rotation.eulerAngles.z);
new WaitForSeconds(1);
canteleport = true;
}
}
}
Comment
Best Answer
Answer by Hellium · Oct 28, 2019 at 10:20 AM
Following code not tested:
using UnityEngine;
public class teleport_player : MonoBehaviour
{
public Transform teleportTarget;
// Static so that all teleporters share the same value
private static float lastTeleportationTime;
void OnTriggerEnter(Collider other)
{
if(Time.time - lastTeleportationTime < 5) // Delay of 5 seconds to prevent infinite loops
return ;
Transform player = other.transform;
player.position = teleportTarget.position;
if ( Mathf.Approximately(teleportTarget.eulerAngles.y, transform.eulerAngles.y) )
player.Rotate(0, 180, 0);
lastTeleportationTime = Time.time;
}
}
What "didn't work"? I have fixed the self.eulerAngles.y
, tested, and it works fine with a very simple scene (one cube, one plane, two trigger objects)