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 :)

  • Hello,

    Hope you all are having a good day.

    I am hoping to get an answer as to what I can do to get the mirror on the left reflect my characters movements as she passes near the area. I watched youtube videos and have seen this post but am still having trouble.

    I using adventure creator on Unity Version 2017 3.1 and am unfamiliar with coding so would like to know if there is a way I can resolve my issue without the need for coding.

    I am linking a link to Imgur below so that your team can get a better understanding of what I am trying to accomplish.

    Please let me know if there is something I can do.

    https://imgur.com/a/would-need-mirror-to-reflect-players-movements-qnl5dIi

    Very Respectfully,
    -Asuna

  • There are two approaches you can take to this.

    One is to have a second character (an NPC) that looks like the Player, and rely on the MirrorCharacter script earlier in the thread to have them move/animate as a reflection of the Player. This requires both characters use Sprites Unity Complex for their animation engine, however.

    The second is to use a Render Texture, as you are. AC doesn't need to be involved here - and you don't need to use Reflection Probes either, that's a separate feature (despite the name).

    You'll need a camera that can output the scene to a RenderTexture - you've done this with your "Camera for render texture", but remove AC's GameCamera2D component (as this'll disable the Camera). Make a separate GameCamera for the MainCamera to follow, and then parent your Render texture camera to it (so that it follows its position).

    You can then display the contents of the RenderTexture on a sprite or quad in the scene, flipped on the X direction so that its mirrored.

  • Okay yes, I can see how I was overthinking it with the reflection probes haha. :D

    After following your steps, I was able to get it to work. Thank you! ^_^*

    -Asuna
  • edited February 7

    Necrobump, I hope I'm not the only one facing this problem; I've tried everything and now I'm desperate.

    I'm using a script from wiki and I can't get it to work (it partially works); the animation only plays in one direction; both sprites use sprites unity complex

    video: https://imgur.com/a/lwpW6Ie

  • You'll need to use Sprites Unity Complex - but its the Direction integer parameter, not the Body Angle float parameter, that gets flipped.

  • You'll need to use Sprites Unity Complex - but its the Direction integer parameter, not the Body Angle float parameter, that gets flipped.

    thank you for your reply

    but I don't quite understand what I need to change, because I took the prefab from the demo and didn't change it in any way.

    demo prefab unity sprites complex player component: https://imgur.com/a/h5crSso

    animator demo prefab sprites unity complex: https://imgur.com/a/fedHkMT

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.