Hey all. I’m creating a game in which a player is able to pass a portal through a portal and back. The scene looks like this:
For some reason, the player does not get teleported to the other portal, unless I walk through the portal very slowly. So the code does function, but just very poorly.
Both portals are made up of a render plane (for the illusion of looking through the portal) and an invisible collider plane. The collider plane has the following script attached:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PortalTeleporter : MonoBehaviour
{
public Transform player;
// Receiver is the position of the collider plane of the other portal
public Transform receiver;
private bool playerIsOverlapping = false;
// Update is called once per frame
void Update() {
if (playerIsOverlapping)
{
Vector3 portalToPlayer = player.position - transform.position;
float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
// If this is true: The player has moved across the portal
if (dotProduct < 0f)
{
//Teleport him!
Debug.Log("Teleport!");
float rotationDiff = -Quaternion.Angle(transform.rotation, receiver.rotation);
rotationDiff += 180;
player.Rotate(Vector3.up, rotationDiff);
Vector3 positionOffset = Quaternion.Euler(0f, rotationDiff, 0f) * portalToPlayer;
player.position = receiver.position + positionOffset;
playerIsOverlapping = false;
}
}
}
void OnTriggerEnter(Collider other)
{
playerIsOverlapping = true;
Debug.Log("Player is now overlapping");
}
void OnTriggerExit(Collider other)
{
playerIsOverlapping = false;
Debug.Log("Player is no longer overlapping");
}
}
Is something wrong with my code? I’ve followed this tutorial step by step, yet the player gets teleported very inconsistently or not at all.
I hope I’ve given you enough information. Thanks a lot for your time and help!