I have made smooth portals in unity and they work like a charm till I rotate them, I was hoping that someone could suggest a different way around the portal rendering issue. Here is my code for the rendering camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PortalCamera : MonoBehaviour
{
public Transform playerCamera;
public Transform portal;
public Transform otherPortal;
void LateUpdate()
{
Vector3 playerOffset2Camera = playerCamera.position - otherPortal.position;
transform.position = portal.position + playerOffset2Camera;
float angleDiffBtwPortalRotations = Quaternion.Angle(portal.rotation, otherPortal.rotation);
Quaternion portalRotationalDiff = Quaternion.AngleAxis(angleDiffBtwPortalRotations, Vector3.up);
Vector3 newCamRotation = portalRotationalDiff * playerCamera.forward;
transform.rotation = Quaternion.LookRotation(newCamRotation, Vector3.up);
}
}
For now the problem is with :
Vector3 playerOffset2Camera = playerCamera.position - otherPortal.position;
transform.position = portal.position + playerOffset2Camera;
Here the script calculates the distance between the player and Portal A and then uses that for the distance in between portal_b and camera_b which is used to render the portal this creates the smooth effect when going through like in the games Portal and Portal 2 by Valve.
The problem I found is that when you rotate the portal the camera does what it is told to do and stays in the position the the player is in but since the portal is rotated it is not at the angle it is supposed to be in and it messes up the whole illusion. I need to find a way to change the rotation of the `offset´ created for the camera so that the illusion is not broken
I have tried multiple things like changing the offset calculation to a GameObject that rotates with the portal but the xyz coordinates don't change so it hasn't worked