Adventure Creator Wikia
Advertisement

This script allows you to create the effect of 2D characters appearing reflected in mirrors - while respecting their facing direction. It works by controlling a second character that is updated each frame according to the original character's state.

To use it:

  1. Set up your character to use Sprites Unity Complex animation.
  2. Duplicate the character you wish to mirror, and keep both characters inside the scene file.
  3. Create a new C# file called MirrorCharacter and attach to the duplicated character.
  4. Assign the two characters in the Mirror Character component's Inspector
  5. Use Unity's Sprite Mask component to limit rendering of the mirror character to the boundary of the mirror.

MirrorCharacter.cs:

using UnityEngine;
using AC;

public class MirrorCharacter : MonoBehaviour
{

	public AC.Char originalCharacter;
	public AC.Char mirrorCharacter;
	public Vector2 positionOffset;

	private void Update ()
	{
		// Position
		mirrorCharacter.Teleport (originalCharacter.transform.position + positionOffset);

		// Move speed
		float moveSpeed = originalCharacter.GetAnimator ().GetFloat (originalCharacter.moveSpeedParameter);
		mirrorCharacter.GetAnimator ().SetFloat (mirrorCharacter.moveSpeedParameter, moveSpeed);

		// Direction
		int direction = originalCharacter.GetAnimator ().GetInteger (originalCharacter.directionParameter);
		int flippedDirection = FlipDirection (direction);
		mirrorCharacter.GetAnimator ().SetInteger (mirrorCharacter.directionParameter, flippedDirection);
	}

	private int FlipDirection (int direction)
	{
		// 0 = Down, 1 = Left, 2 = Right, 3 = Up, 4 = DownLeft, 5 = DownRight, 6 = UpLeft, 7 = UpRight
		switch (direction)
		{
			case 0:
				return 3;

			case 1:
				return 2;

			case 2:
				return 1;

			case 3:
				return 0;

			case 4:
				return 7;

			case 5:
				return 6;

			case 6:
				return 5;

			case 7:
				return 4;

			default:
				return direction;
		}
	}

}
Advertisement