Page 1 of 1

ScenePortal and setting character facing direction in 2D game

Posted: Thu Jun 24, 2021 2:38 pm
by jlhacode
Hi Tony,

My character can only face left & right, and the prefab has a isFacingLeft bool that controls the direction that the character is facing. It faces left by default

When entering a scene from the left, I would like my character to enter the scene facing right (isFacingLeft = false). What's your opinion on implementing this when using a ScenePortal?

Re: ScenePortal and setting character facing direction in 2D game

Posted: Thu Jun 24, 2021 2:58 pm
by Tony Li
Hi,

Here's one way to do it:

1. Add information to the spawnpoint to specify the player should face left or right. For example, you could add tags "FaceLeft" and "FaceRight", or you could add a simple script:

Code: Select all

public class SpawnpointDirection : MonoBehaviour
{
    public bool faceLeft;
}
2. Make a subclass of PositionSaver. Override ApplyData. Example using the SpawnpointDirection script above:

Code: Select all

public class CustomPlayerPositionSaver : PositionSaver
{
    public override void ApplyData(string s)
    {
        if (usePlayerSpawnpoint && SaveSystem.playerSpawnpoint != null)
        {
            var spawnpointDirection = SaveSystem.playerSpawnpoint.GetComponent<SpawnpointDirection>();
            if (spawnpointDirection != null)
            {
                GetComponent<YourCharacterScript>().isFacingLeft = spawnpointDirection.faceLeft;
            }
        }
        base.ApplyData(s);
    }
}
In your subclass, you could also save and restore the player's facing direction for use in saved games.

Re: ScenePortal and setting character facing direction in 2D game

Posted: Thu Jun 24, 2021 4:17 pm
by jlhacode
That worked perfectly, thanks Tony!

Re: ScenePortal and setting character facing direction in 2D game

Posted: Thu Jun 24, 2021 5:20 pm
by Tony Li
Awesome! Glad to help.