Forum rules - please read before posting.

How to make a working mirror in a 2D game?

2»

Comments

  • You are a legend Chris! Truly!

  • Hey we got it working perfectly!

  • Looking good! I've added the script to the AC wiki.

  • Good evening!

    I'm trying to achieve this same effect but I can't make it work. This is what I did.
    I've added the Player to the scene (unpacked the prefab), added the script to the parent, chose this character as the target and the player on start to get the player.
    I've kept all the player properties in the clone (even the animator with unity complex) but the clone doesn't do anything. It overlaps with the player and just does nothing (basically idle). It's like it never detects the actual player.

    I also found that in the script Vector2 should be Vector3 or at least that way, it works. If not, it creates an error.

    I also added the target within the sprite mask component and I also failed here because the mirror in on the bottom and want to actually move it above hip.

    I'm not sure if you want some proof or more details. Let me know.

    Thanks a lot guys. This feature is super important to have for almost all games.

  • I've updated the script to avoid the need for the mirror character to have an AC Player or NPC component - try it now and share screenshots if you have any issues.

  • Hello.
    Unfortunately, no luck :(
    I tried to make it simple in pictures.

    https://imgur.com/a/t8craYA

    Thanks!

  • The script required both characters to be placed in the scene file, with the "Original Character" field mapped to the character in the scene Hierarchy - not the original prefab.

    I have updated the script with an "Is Player" checkbox you can use in the case of a Player prefab spawned in at runtime.

  • Hello Chris,

    No luck with this and no new results. Let me show you what we did because we can't make it work.

    First, I've added to the scene, a "player" with the player script (but deactivated), along with the script and the Sprite Mask. Under this "player" (the mirrored player), the animator and the sprite renderer (all of this is just the unpacking of the prefab of my real player).

    When the scene starts, the mirrored player overlaps with the real player. And of course it never disappears

    Besides, it also makes no sense how it can reflect only on a sprite inside the scenario when they are game objects and the Sprite Mask doesn't allow to attach GameObject. Maybe there's a way to tell that it only renders on a certain surface...

    I don't know how you guys made it work. It looks so simple!
    https://imgur.com/a/8ZUuR7o

    Thanks

  • You can remove the Player component, not just disable it.

    The script appears to be working in the sense that it's now sticking to the Player character - you need to play with the Position Offset field in its Inspector to move it to the side.

    The script does not deal with masking - that's all handled by how you set up the Sprite Mask. I'd suggest getting the mask working without the script's / AC's involvement first.

  • Sir, you are the best. I will post results here later when I make it work. Maybe some other user will come here eventually :)

  • This is how I made it work. Fist, I've added at the bottom of the script this little modification (I wanted an effect ON a mirror, not mirror the character).

            case 1:
                return 1; //2 for mirroring the frame
    
            case 2:
                return 2;//1 for mirroring the frame
    

    I'm missing the ones on the back but I don't have those frames yet. Eventually will have to modify some others.

    Then, I've added triggers which changes the offset so the mirrors are different depending on how far or close it is (this is activated through object-sendmessage and then the name of the method:

    public void ModifyOffset01()
    {
        positionOffset = new Vector3(0, 0, 0);
    }
    

    This is also helpful if you want to create a semi round effect in which each trigger modifies a bit the new Vector on the X axis. Not super easy but with 4 or 5 triggers you can create different nice effects.


    On Unity, I've grabbed the mirror, then cut only the glass part with another software (if not, then the character will show even on the sides of the mirror).

    This mirror (the complete one) has only the sprite renderer and the Sprite Mask components. The Sprite Mask has to have selected the glass I just cut, so the part in which we will see the character is only there and not around the frame of the mirror.

    As for the character, I went to the child (the sprite) and chose Mask Interaction: Visible Inside Mask. And the sorting layer is the same one as the mirror ("background front" in our case).

    As for the lighting or effects, all done through sorting layers and lighting only affecting there.

    Hope it helps guys!

  • Me again. I got a question (then I will give specific details on the why).
    How can I flip the character on the X axis (just the sprite) on some of those cases?

    Example: Let's say you have a character with a robotic right arm. If the character is facing left and the right arm points at the mirror, then the mirror will show the right arm on the right side. The thing I thought is to actually show the "case 1 returning 2" and "case 2 returning 1" but flipping the X axis so the right arm is the left arm on the mirror. Makes sense?

    Maybe there's a way to call something on those cases to flip this mirror character.
    Hope it's clear :)

    Thanks a lot.

  • "case 1 returning 2" is there to modify the sprite direction from left to right by adjusting the Animator's "Direction integer" parameter. If you want to modify the direction of the sprite manually, you can do it either by inverting the scale:

    transform.localScale = new Vector3 (-1f, 1f, 1f);
    

    Or by checking the "Flip X" option in the Sprite Renderer:

    GetComponentInChildren<SpriteRenderer> ().flipX = true;
    
  • Thanks a lot :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.