Wednesday, October 19, 2022
HomeGame Developmentunity - Align Relative Portal Digital camera Place

unity – Align Relative Portal Digital camera Place


I have 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 Participant Cam. Then apply the distance and rotation to my OtherPortal 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, otherPortal.rotation);
d = Mathf.Sqrt( (playerCam.place.x - portal.place.x)^2 +
                (playerCam.place.z - portal.place.z)^2 );
if(playerCam.place.x >= portal.place.x)
    d = -d;
phi = Mathf.Atan( (playerCam.place.z - portal.place.z)
                / (playerCam.place.x - portal.place.x) );

newX = otherPortal.place.x - d*Mathf.Sin(phi - delta);
newY = playerCam.place.y + otherPortal.place.y - portal.place.y;
newZ = otherPortal.place.z + d*Mathf.Cos(phi - delta);
rework.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 greatest repair this bug?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments