2D Teleporter Delay Issues

Hey all,

I’ve been applying the Brackey’s Portal YouTube tutorial to my game, making adjustments as mine is 2D. It works with the one exception of an intermittent delay between when the player enters the trigger2D collider and when the portal action actually takes place.

Horizontal Portal Setup
Player enters Left Portal and exits out of Right Portal.

Through testing, I’ve noticed this issue is highly reproducible when the player enters the portal at a fast speed and at an angle. Slow and head-on works as intended 100% of the time. Even odder is I have a separate scene with vertical portals (enter from the bottom exit from the top) and that works 100% of the time no matter the speed of entry, it’s just these “horizontal” portals that are giving me issues.

Here’s the code for the Portal.


    public Transform player;
    public Transform player2;
    public Transform receiver;

    public bool playerIsOverlapping = false;
    public bool player2IsOverlapping = false;

    private float teleportRate = 100f;
    private float teleportTime = 0f;

    // Update is called once per frame
    void Update()
    {
        if (playerIsOverlapping)
        {
            Vector2 portalToPlayer = player.position - transform.position;
            float dotProduct = Vector2.Dot(transform.up, portalToPlayer);

            if (dotProduct < 0f)
            {
                float rotationDiff = -Quaternion.Angle(transform.rotation, receiver.rotation);
                rotationDiff += 180f;

                Vector3 positionOffset = Quaternion.Euler(0f, rotationDiff, 0f) * portalToPlayer;
                player.position = receiver.position + positionOffset;

                playerIsOverlapping = false;
                FindObjectOfType<AudioManager>().Play("environment_teleport");
            }
        }

        if (player2IsOverlapping)
        {
            Vector2 portalToPlayer = player2.position - transform.position;
            float dotProduct = Vector2.Dot(transform.up, portalToPlayer);

            if (dotProduct < 0f)
            {
                float rotationDiff = -Quaternion.Angle(transform.rotation, receiver.rotation);
                rotationDiff += 180f;

                Vector3 positionOffset = Quaternion.Euler(0f, rotationDiff, 0f) * portalToPlayer;
                player2.position = receiver.position + positionOffset;

                player2IsOverlapping = false;
                FindObjectOfType<AudioManager>().Play("environment_teleport");
            }
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player") && Time.time >= teleportTime)
        {
            playerIsOverlapping = true;
            GameObject.Find("Player_1").SendMessage("TeleportPlayer");
            teleportTime = Time.time + 1f / teleportRate;
            Debug.Log("Teleported");
        }

        if (other.gameObject.CompareTag("Player2") && Time.time >= teleportTime)
        {
            player2IsOverlapping = true;
            GameObject.Find("Player_2").SendMessage("TeleportPlayer");
            teleportTime = Time.time + 1f / teleportRate;
            Debug.Log("Teleported");
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            playerIsOverlapping = false;
        }

        if (other.gameObject.CompareTag("Player2"))
        {
            player2IsOverlapping = false;
        }
    }

More details after further debugging:

There is a delay in communication between the initial “OnTriggerEnter2D” function and the “PlayerIsOverlapping” function when the player enters the portal at an increased speed. When entering at a moderate/slow speed, the communication is instantaneous.