Search Unity

2D Portals

Discussion in '2D' started by isse26, Mar 17, 2014.

  1. isse26

    isse26

    Joined:
    Dec 8, 2013
    Posts:
    30
    Hi, I'm working on a 2D Puzzle game with portals. I need to create portals that can send objects with specific tags through and have them keep there velocety and preferably there rotation. Similar to portals in "Portal". I have figured out a way to make portals in the 3D mode. Here are some code that I'm using for that. Please help me.

    Code (csharp):
    1. public class Portal : MonoBehaviour
    2. {
    3.  
    4.     public GameObject otherEnd = null; // The other portal
    5.     public bool isTeleporting = false;
    6.  
    7.     void OnTriggerExit(Collider c)
    8.     {
    9.         // Checking if the thig that entered can teleport
    10.         if (c.tag == "Player" || c.tag == "Portabel")
    11.         {
    12.             if (!isTeleporting)
    13.             {
    14.                 otherEnd.GetComponent<Portal>().TeleportTo(c.gameObject);
    15.             }
    16.             isTeleporting = false;
    17.         }
    18.     }
    19.  
    20.     public void TeleportTo(GameObject g)
    21.     {
    22.         // Teleporting
    23.         g.transform.position = transform.position - (transform.up*0.65f);
    24.         g.rigidbody.velocity = transform.up * g.rigidbody.velocity.magnitude;
    25.  
    26.         isTeleporting = true;
    27.     }
    28.  
    29. }
    30.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,411