Home Game Development unity – Align Relative Portal Camera Position

unity – Align Relative Portal Camera Position

0
unity – Align Relative Portal Camera Position

[ad_1]

I’ve been having a little bit of bother with this small piece of code. To set my relative PortalCam place, I first take the distance and angle from the Portal and Player Cam. Then apply the distance and rotation to my DifferentPortal to get the PortalCams place.

Desmos hyperlink to all of the maths

float newX, newY, newZ;
float d;    // Distance between Portal and PlayerCam
float phi;  // Angle between Portal and PortalCam
            // Angle between Portals
float delta = Quaternion.Angle(portal.rotation, differentPortal.rotation);
d = Mathf.Sqrt( (participantCam.place.x - portal.place.x)^2 +
                (participantCam.place.z - portal.place.z)^2 );
if(participantCam.place.x >= portal.place.x)
    d = -d;
phi = Mathf.Atan( (participantCam.place.z - portal.place.z)
                / (participantCam.place.x - portal.place.x) );

newX = differentPortal.place.x - d*Mathf.Sin(phi - delta);
newY = participantCam.place.y + differentPortal.place.y - portal.place.y;
newZ = differentPortal.place.z + d*Mathf.Cos(phi - delta);
remodel.place = new Vector3(newX, newY, newZ);

My drawback is that, whereas this works completely when the portals are at 90degrees to one another. The code appears to fail when the portals are in-line.

Is there a mistake in my math? In my code? How can I finest repair this bug?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here