private void SearchForPortal(ref Traveler traveler, float3 characterPosition) { traveler.hasChecked = 0; // find new portal if (UnityEngine.Time.time - traveler.lastCheckedClosestPortal >= 1f) { traveler.lastCheckedClosestPortal = UnityEngine.Time.time; var portals = portalQuery.ToComponentDataArray <Translation>(Allocator.TempJob); float closestDistance = PortalSystem.portalOpenDistance; int closestIndex = -1; float3 closestPosition = float3.zero; for (int i = 0; i < portals.Length; i++) { // distance to float newDistance = math.distance(portals[i].Value, characterPosition); if (newDistance < closestDistance) { closestDistance = newDistance; closestIndex = i; closestPosition = portals[i].Value; } } if (closestIndex != -1) { var portalEntities = portalQuery.ToEntityArray(Allocator.TempJob); if (!World.EntityManager.Exists(traveler.portal) || traveler.portal.Index != portalEntities[closestIndex].Index) { if (World.EntityManager.Exists(traveler.portal)) { World.EntityManager.SetComponentData(traveler.portal, new Translation { Value = traveler.originalPortalPosition }); } if (World.EntityManager.Exists(traveler.portal)) { World.EntityManager.SetComponentData(traveler.portal, new Translation { Value = traveler.originalPortalPosition }); } traveler.portal = portalEntities[closestIndex]; traveler.originalPortalPosition = closestPosition; traveler.portalSide = SideOfPortal(characterPosition, closestPosition, World.EntityManager.GetComponentData <LocalToWorld>(traveler.portal).Forward); } portalEntities.Dispose(); } portals.Dispose(); } }
private void RepositionPortal(Camera playerCam, Traveler traveler, float3 cameraPosition, float3 portalForward) { const float portalRepositionDistance = 0.5f; // 0.5 float nearClipPlane = playerCam.nearClipPlane + nearClipPlaneAddition; float halfHeight = nearClipPlane * Mathf.Tan(playerCam.fieldOfView * portalRepositionDistance * Mathf.Deg2Rad); float halfWidth = halfHeight * playerCam.aspect; float dstToNearClipPlaneCorner = new Vector3(halfWidth, halfHeight, playerCam.nearClipPlane).magnitude; float screenThickness = dstToNearClipPlaneCorner; //Transform screenT = screen.transform; bool camFacingSameDirAsPortal = math.dot(portalForward, traveler.originalPortalPosition - cameraPosition) > 0; float3 newPosition = portalForward * screenThickness * ((camFacingSameDirAsPortal) ? portalRepositionDistance : -portalRepositionDistance); // Vector3.forward World.EntityManager.SetComponentData(traveler.portal, new Translation { Value = traveler.originalPortalPosition + newPosition }); }
private void TeleportTraveler(ref Translation travelerPosition, ref Rotation travelerRotation, ref Traveler traveler, Entity travelerEntity) //, float3 characterPosition, float3 portalPosition) { // Traveler var travelerMatrix = World.EntityManager.GetComponentData <LocalToWorld>(travelerEntity).Value; // Portal Entity portalEntity = traveler.portal; var portalPosition = World.EntityManager.GetComponentData <Translation>(portalEntity).Value; var portalRotation = World.EntityManager.GetComponentData <Rotation>(portalEntity).Value; var portalMatrixInverse = math.inverse(World.EntityManager.GetComponentData <LocalToWorld>(portalEntity).Value); // linked portal var linkedPortalEntity = World.EntityManager.GetComponentData <Portal>(portalEntity).linkedPortal; var linkedPortalPosition = World.EntityManager.GetComponentData <Translation>(linkedPortalEntity).Value; var linkedPortalRotation = World.EntityManager.GetComponentData <Rotation>(linkedPortalEntity).Value; var linkedPortalMatrix = World.EntityManager.GetComponentData <LocalToWorld>(linkedPortalEntity).Value; float3 offsetFromPortal = travelerPosition.Value - portalPosition; float3 newOffset = math.mul(linkedPortalRotation, (math.mul(math.inverse(portalRotation), offsetFromPortal))); travelerPosition.Value = linkedPortalPosition + newOffset; quaternion newRotation = math.mul((linkedPortalRotation), (math.mul(math.inverse(portalRotation), travelerRotation.Value))); travelerRotation.Value = newRotation; traveler.lastCheckedClosestPortal = UnityEngine.Time.time - 1f; }