Portal recreation Camera Orientation when exiting a portal

So I’m in the process of recreating the game Portal and I have most of the portal mechanics sorted out, but there’s been one major issue throughout developing it - the camera needs to face the correct direction when you pass through portals.

So I made some code here, which basically gets the difference between the two portals angles and applies that onto the camera’s CFrame. This works, but there’s an issue:

diff = portals.OrangePortal.ViewportCenter.Orientation - 
            portals.BluePortal.ViewportCenter.Orientation
-- Get the difference between the orange and blue portal angles, relative to orange
     				
ry = diff.Y
     					
local angleDiff = CFrame.Angles(0, math.rad(ry), 0) -- convert to rad
     					
game.Workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * angleDiff
-- Apply this to the player's camera

The issue here is that when you leave the portal, your Y angle will be correct, but the Vertical angle (I think this is X?) is inverted, therefore entering a portal facing down will make you exit facing upwards instead. There is also another issue in that this only works if both portals have a difference of a multiple of 180 (e.g. blue 180 and orange 180), if one is 90 degrees to another the player will face as if the portals are 180 degrees to one another.

I’ve tried inverting the degrees, setting the other angles to 0 (as seen in the code) and just negating them, but every time the result is the same: your vertical angle is wrong.

How can I make it so that the player will face the correct way when exiting a portal?

I would post a video but the DevForum never lets me do that, so… if you know why let me know and I’ll try again I suppose

You can try taking a look at this place to see how i transitioned the camera and character through the portal.

2 Likes

ok, thanks. I’ll take a look!

(also your viewports are fantastic btw, they’re so good!)

Thanks so much @EgoMoose! Using this part here

local camOffset = portals.OrangePortal.Center.CFrame:Inverse() * workspace.CurrentCamera.CFrame
local newCam = portals.BluePortal.Center.CFrame * Y_SPIN * camOffset
					
workspace.CurrentCamera.CFrame = newCam

I got it to work! Thanks!

Also, I found a bug in your place. Sometimes, walking into the portal doesn’t do anything and then you can walk backwards through it.

I’m going to put you in the credits of this game when I finally get it out because I’ve used a fair bit of your work and modified it, its incredible some of the stuff you do :slight_smile:

3 Likes

Yeah the passing through the portal thing is bc i use an intersection of the velocity vector with the plane, but sometimes it doesn’t register bc the ray is too short. It’s kinda a hard fix bc if u extend the ray to make it more consistent then you teleport earlier.

Anyways, good luck!

2 Likes