Creating a Portal System in Unity

Portals are a popular feature in many games, enabling players to teleport between different locations seamlessly. In this tutorial, we'll explore various techniques for creating a portal system in Unity. We will cover basic teleportation, using render textures for visual portals, and implementing portal mechanics that maintain player orientation and momentum.

Setting Up the Project

To get started, let's set up a basic Unity project:

  1. Create a new Unity project.
  2. Add a new folder named Scripts to organize our scripts.
  3. Create a new 3D scene with a few basic objects, including a player character and two portal objects.

Basic Teleportation

The simplest form of a portal system is basic teleportation, where the player instantly moves from one location to another.

Creating the Teleportation Script

using UnityEngine;

public class TeleportationPortal : MonoBehaviour
{
    public Transform destination;

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            other.transform.position = destination.position;
            other.transform.rotation = destination.rotation;
        }
    }
}

Attach this script to both portal objects and assign the destination to the corresponding portal.

Using Render Textures for Visual Portals

To create a more immersive portal system, we can use render textures to show what's on the other side of the portal.

Setting Up the Render Textures

  1. Create a new Render Texture by right-clicking in the Project window and selecting Create > Render Texture.
  2. Repeat this to create a second Render Texture.
  3. Create two new cameras in the scene, one for each portal, and assign each camera a Render Texture.
  4. Set the cameras' positions to match the destinations of the portals.

Applying the Render Textures

using UnityEngine;

public class Portal : MonoBehaviour
{
    public Camera portalCamera;
    public Material portalMaterial;

    void Start()
    {
        portalMaterial.mainTexture = portalCamera.targetTexture;
    }
}

Attach this script to each portal and assign the corresponding portal camera and material with the Render Texture.

Maintaining Player Orientation and Momentum

To make the portal system more realistic, we need to maintain the player's orientation and momentum when they pass through a portal.

Enhanced Teleportation Script

using UnityEngine;

public class EnhancedPortal : MonoBehaviour
{
    public Transform destination;

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            CharacterController playerController = other.GetComponent();
            Rigidbody playerRigidbody = other.GetComponent();

            // Disable the CharacterController to allow manual position and rotation updates
            if (playerController != null)
            {
                playerController.enabled = false;
            }

            // Maintain orientation
            Vector3 relativePosition = destination.InverseTransformPoint(other.transform.position);
            other.transform.position = destination.TransformPoint(relativePosition);

            // Maintain momentum
            if (playerRigidbody != null)
            {
                Vector3 relativeVelocity = destination.InverseTransformDirection(playerRigidbody.velocity);
                playerRigidbody.velocity = destination.TransformDirection(relativeVelocity);
            }

            // Re-enable the CharacterController
            if (playerController != null)
            {
                playerController.enabled = true;
            }
        }
    }
}

Attach this script to each portal and assign the corresponding destination.

Testing the Portal System

To test the portal system, follow these steps:

  1. Place the player character near one of the portals.
  2. Press Play to run the game.
  3. Move the player character into the portal and observe the teleportation and visual effects.

Conclusion

We've explored various techniques for creating a portal system in Unity. We started with basic teleportation, added visual portals using render textures, and enhanced the system to maintain player orientation and momentum. These concepts can be further expanded and customized to fit the needs of your specific game project.

Links
Unity