Exemple #1
0
    private void UpdateTeleport()
    {
        if (!enableTeleportation)
        {
            return;
        }

        for (int i = 0; i < objectsInPortal.Count; i++)
        {
            // Check if portalable object is behind the portal
            // If so, we can assume they have crossed through the portal.
            // Implying from this, you should not be able to touch a portal from behind.
            // This can be changed later to allow portals to be touched from behind, but no support for now.
            Vector3 objPosRelativeToPortalNormal = portalNormal.transform.InverseTransformPoint(objectsInPortal[i].transform.position);
            if (objPosRelativeToPortalNormal.z < 0)
            {
                //Debug.Log("Object warped!");

                // NavMeshAgent support part 1
                NavMeshAgent navMeshAgent = objectsInPortal[i].GetComponent <NavMeshAgent>();
                if (navMeshAgent)
                {
                    navMeshAgent.enabled = false;
                }

                // Warp object
                objectsInPortal[i].transform.SetPositionAndRotation(
                    TransformPositionBetweenPortals(this, target, objectsInPortal[i].transform.position),
                    TransformRotationBetweenPortals(this, target, objectsInPortal[i].transform.rotation));

                // NavMeshAgent support part 2
                if (navMeshAgent)
                {
                    navMeshAgent.enabled = true;
                }

                // AIInput support
                AIInput aiInput = objectsInPortal[i].GetComponent <AIInput>();
                if (aiInput)
                {
                    /*if (aiInput.AttemptChangePathCurrentCorner(target.navMeshLinkGuide.position, indexOffset: 1))
                     * {
                     *  Debug.Log("FAILED");
                     * }
                     * else
                     * {
                     *  Debug.Log("SUCCESS");
                     * }*/
                    aiInput.ForcePathRecalculate();
                }

                // Rigidbody support
                Rigidbody rigidbody = objectsInPortal[i].GetComponent <Rigidbody>();
                if (rigidbody && rigidbody.isKinematic == false)
                {
                    rigidbody.velocity = TransformDirectionBetweenPortals(this, target, rigidbody.velocity);
                }

                // Update physics transforms after warp
                Physics.SyncTransforms();

                // Object is no longer in this side of the portal
                objectsInPortal.RemoveAt(i);
            }
        }
    }