Search Unity

Creating portals

Discussion in 'Scripting' started by wwkjohn_unity, Jan 19, 2019.

  1. wwkjohn_unity

    wwkjohn_unity

    Joined:
    Jan 17, 2019
    Posts:
    2
    Hi,

    I am trying to make a portal like the following video, however, when I turn the receiving portal by 90 degree, the player is teleported but the direction of the camera did not rotate accordingly (i.e. by 90 degree). How should I modify the following code for that? I am a beginner in Unity please help.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TeleportPlane2 : MonoBehaviour
    6. {
    7.  
    8.     public Transform player;
    9.     public Transform receiver;
    10.  
    11.     private bool playerIsOverlapping = false;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (playerIsOverlapping)
    17.         {
    18.             Vector3 portalToPlayer = player.position - transform.position;
    19.             float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
    20.  
    21.             //If this is true: The player has moved across the portal
    22.             if (dotProduct < 0f)
    23.             {
    24.                 //Teleport him!
    25.                 float rotationDiff = -Quaternion.Angle(transform.rotation, receiver.rotation);
    26.                 rotationDiff += 180;
    27.                 player.Rotate(Vector3.up, rotationDiff);
    28.  
    29.                 Vector3 positionOffset = Quaternion.Euler(0f, rotationDiff, 0f) * portalToPlayer;
    30.                 player.position = receiver.position + positionOffset;
    31.  
    32.                 playerIsOverlapping = false;
    33.  
    34.  
    35.             }
    36.         }
    37.     }
    38.  
    39.     void OnTriggerEnter(Collider collider)
    40.     {
    41.         if (collider.name == "Player")
    42.         {
    43.             playerIsOverlapping = true;
    44.         }
    45.     }
    46.     void OnTriggerExit(Collider collider)
    47.     {
    48.         if (collider.name == "Player")
    49.         {
    50.             playerIsOverlapping = false;
    51.         }
    52.  
    53.     }
    54. }
    55.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,528