FANDOM


This script allows for first-person navigation in 2D.  It works by snap-cutting to cameras over different backgrounds that each represent one of the four cardinal directions (north, south, east and west).

To use it:

  1. Create a new C# script named CameraNode2D.cs, and paste in the code below
  2. Create a new empty GameObject named "Node" and attach the script as a component
  3. Add four background sprites (one for each direction) as child GameObjects, and arrange them next to each other
  4. Add four Cameras, also as child GameObjects, and arrange them so that each faces one of the four backgrounds
  5. Add the "Basic Camera" component to each Camera GameObject via the Inspector's "Add Component" menu
  6. In the Camera Node 2D Inspector, assign each Camera to the relevant direction field
  7. Make a prefab out of this whole GameObject, so that it can be recycled
  8. To connect two nodes together, assign the "Node" field in the Camera Node 2D Inspector for the direction of travel
  9. Use the Menu Manager to create a new Menu named "Navigation", and set its "Appear type" to "During Gameplay"
  10. Within that Menu, create any of the four Buttons named as follows: "Forward", "Left", "Right", "Backward". Style the Menu as required
  11. In the Scene Manager, assign any of the Cameras as the game's Default Camera

When clicked, you should then be able to use the Menu buttons to switch camera, and hence navigate the scene

CameraNode2D.cs:

using UnityEngine;
using System.Collections;

namespace AC
{

public class CameraNode2D : MonoBehaviour
{

#region Variables

[SerializeField] private NodeDirection2D north;
[SerializeField] private NodeDirection2D east;
[SerializeField] private NodeDirection2D south;
[SerializeField] private NodeDirection2D west;

private enum FacingDirection2D { None, North, East, South, West };
private FacingDirection2D direction = FacingDirection2D.None;

private static CameraNode2D activeNode;

#endregion


#region UnityStandards

private void OnEnable ()
{
EventManager.OnSwitchCamera += OnSwitchCamera;
EventManager.OnMenuElementClick += OnMenuElementClick;
}


private void OnDisable ()
{
EventManager.OnSwitchCamera -= OnSwitchCamera;
EventManager.OnMenuElementClick -= OnMenuElementClick;
}

#endregion


#region CustomEvents

private void OnSwitchCamera (_Camera fromCamera, _Camera toCamera, float transitionTime)
{
if (north.IsMatch (toCamera))
{
direction = FacingDirection2D.North;
}
else if (east.IsMatch (toCamera))
{
direction = FacingDirection2D.East;
}
else if (south.IsMatch (toCamera))
{
direction = FacingDirection2D.South;
}
else if (west.IsMatch (toCamera))
{
direction = FacingDirection2D.West;
}
else
{
direction = FacingDirection2D.None;
}

if (direction != FacingDirection2D.None)
{
activeNode = this;
}
else if (activeNode == this)
{
activeNode = null;
}
}


private void OnMenuElementClick (Menu menu, MenuElement element, int slot, int buttonPressed)
{
if (activeNode == this)
{
if (menu.title == "Navigation")
{
if (element.title == "Forward")
{
MoveForward ();
}
else if (element.title == "Left")
{
TurnLeft ();
}
else if (element.title == "Right")
{
TurnRight ();
}
else if (element.title == "Backward")
{
MoveBackward ();
}
}
}
}

#endregion


#region PrivateFunctions

private void MoveForward ()
{
GetDirectionNode (direction).MoveForward (direction);
}


private void MoveBackward ()
{
GetReverseDirectionNode (direction).MoveForward (direction);
}


private void TurnLeft ()
{
switch (direction)
{
case FacingDirection2D.North:
SetFacingDirection (FacingDirection2D.West);
break;

case FacingDirection2D.East:
SetFacingDirection (FacingDirection2D.North);
break;

case FacingDirection2D.South:
SetFacingDirection (FacingDirection2D.East);
break;

case FacingDirection2D.West:
SetFacingDirection (FacingDirection2D.South);
break;

default:
break;
}
}


private void TurnRight ()
{
switch (direction)
{
case FacingDirection2D.North:
SetFacingDirection (FacingDirection2D.East);
break;

case FacingDirection2D.East:
SetFacingDirection (FacingDirection2D.South);
break;

case FacingDirection2D.South:
SetFacingDirection (FacingDirection2D.West);
break;

case FacingDirection2D.West:
SetFacingDirection (FacingDirection2D.North);
break;

default:
break;
}
}


private void SetFacingDirection (FacingDirection2D newDirection)
{
direction = newDirection;
GetDirectionNode (direction).Face ();
}


private _Camera GetCamera (FacingDirection2D _direction)
{
return GetDirectionNode (_direction).Camera;
}


private NodeDirection2D GetDirectionNode (FacingDirection2D _direction)
{
switch (_direction)
{
case FacingDirection2D.North:
return north;

case FacingDirection2D.East:
return east;

case FacingDirection2D.South:
return south;

case FacingDirection2D.West:
return west;

default:
return null;
}
}


private NodeDirection2D GetReverseDirectionNode (FacingDirection2D _direction)
{
switch (_direction)
{
case FacingDirection2D.North:
return south;

case FacingDirection2D.East:
return west;

case FacingDirection2D.South:
return north;

case FacingDirection2D.West:
return east;

default:
return null;
}
}

#endregion


#region PrivateClasses

[System.Serializable]
private class NodeDirection2D
{

[SerializeField] private _Camera camera;
[SerializeField] private CameraNode2D node;


public bool IsMatch (_Camera _camera)
{
return (camera == _camera);
}


public void Face ()
{
if (camera != null)
{
KickStarter.mainCamera.SetGameCamera (camera);
}
}


public void MoveForward (FacingDirection2D direction)
{
if (node != null)
{
_Camera newCamera = node.GetCamera (direction);
KickStarter.mainCamera.SetGameCamera (newCamera);
}
}


public _Camera Camera
{
get
{
return camera;
}
}

}

#endregion

}

}
Community content is available under CC-BY-SA unless otherwise noted.