Railcam 2D
Store

Custom Component Integration

The core Railcam 2D Component is optional, as Railcam 2D provides an API that allows easy integration with an existing camera component.

This means you can take full advantage of Railcam 2D's rail system while using your own component to move the camera.

This guide details a simple integration with a custom component.

Table of Contents


Integrating with Railcam 2D

Railcam 2D provides an API that you can use to calculate camera position. This API is used by the main Railcam 2D component, and can be accessed in the static class Railcam2D.RailUtilities.

In your custom component, import the Railcam 2D namespace:

using Railcam2D;

The RailUtilities class exposes the method GetCameraPosition:

// RailUtilities.cs
public static Vector2 GetCameraPosition(Vector2 targetPosition, Rail[] rails);

GetCameraPosition is used to calculate camera position on a Rail. This method expects an array of Rail objects and a Vector2 that represents the target object's position. It returns another Vector2 that represents the position on a Rail to which the camera should be moved.

You can get an array of all Rails in the scene using Object.FindObjectsOfType. This is computationally expensive, so it's recommended that you do this once inside Awake and cache the resulting array as a private variable in your component:

private Rail[] _rails;

private void Awake()
{
    _rails = Object.FindObjectsOfType<Rail>();
}

Since we cache Rails on initialization, any following Rails added to the scene will not be in the array and therefore not be used for camera position calculations. If you add a Rail to the scene after your camera component is initialized, make sure you also update the cache.

GetCameraPosition should be called in your update loop.

Here's a simple component that uses Railcam 2D's rail system to calculate camera position:

using Railcam2D;
using UnityEngine;

public class CustomComponent : MonoBehaviour
{
    private Rail[] _rails;

    // Target object's Transform
    public Transform Target;

    private void Awake()
    {
        _rails = Object.FindObjectsOfType<Rail>();
    }

    private void LateUpdate()
    {
        var newPosition = RailUtilities.GetCameraPosition(Target.position, _rails);

        // Set camera position
        transform.position = new Vector3(
            newPosition.x,
            newPosition.y,
            transform.position.z);
    }

    // Optional function to update Rail cache
    public void FindRails()
    {
        _rails = Object.FindObjectsOfType<Rail>();
    }
}

Now it's time to create some Rails!


Creating Rails

Creating and editing Rails is done in the same way as before - using the Rail Manager component.

The only difference is the way in which you add the Rail Manager to the scene.

Instead of using the button in the editor of the Railcam 2D Component, you should either drag the Railmanager.cs component file from Railcam 2D's root directory and drop it onto your component, or use the "Add Component" button in the Inspector and search for "Rail Manager".

See the Rail Manager Component and Rail Component documentation for more information.

Jonathan Madelaine © 2020