ScenePortal and setting character facing direction in 2D game

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
jlhacode
Posts: 77
Joined: Fri Jul 03, 2020 6:23 am

ScenePortal and setting character facing direction in 2D game

Post 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?
User avatar
Tony Li
Posts: 21989
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
jlhacode
Posts: 77
Joined: Fri Jul 03, 2020 6:23 am

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

Post by jlhacode »

That worked perfectly, thanks Tony!
User avatar
Tony Li
Posts: 21989
Joined: Thu Jul 18, 2013 1:27 pm

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

Post by Tony Li »

Awesome! Glad to help.
Post Reply