Character portraits can be placed inside a Subtitles menu (see this tutorial). This script causes them to be flipped horizontally whenever the character is facing sceen-left, so that their portrait is consistent with their facing direction.
To use it:
- Configure your Subtitles menu with a portrait graphic, and have the Menu use Unity UI.
- Copy/paste the code below into a C# script named FlipPortraitLeft.
- Attach the Flip Portrait Left component to your UI Canvas prefab, and assign both the Canvas, and the portrait Image components, in its Inspector.
- Adjust your portrait graphics so that they all face screen-right.
FlipPortraitLeft.cs:
using UnityEngine;
using UnityEngine.UI;
using AC;
public class FlipPortraitLeft : MonoBehaviour
{
public Canvas canvas;
public Image portraitImage;
void Update ()
{
Speech speech = KickStarter.dialog.GetLatestSpeech ();
Menu menu = KickStarter.playerMenus.GetMenuWithCanvas (canvas);
if (menu != null && menu.speech != null)
{
speech = menu.speech;
}
if (speech == null) return;
Char speaker = speech.GetSpeakingCharacter ();
if (speaker == null ) return;
bool isFacingRight = Vector3.Dot (speaker.TransformForward, Camera.main.transform.right) > 0f;
Vector3 scale = portraitImage.rectTransform.localScale;
if (isFacingRight)
{
if (scale.x < 0f) scale.x *= -1f;
}
else
{
if (scale.x > 0f) scale.x *= -1f;
}
portraitImage.rectTransform.localScale = scale;
}
}