Forum rules - please read before posting.

Order in layer problems with mechanim character

Hi, I have been working on animating my character using a heirarchy of sprites and an IK script in mechanim.

Until now my character has only been placeholder images for his directions so he changes/flips correctly when moving around the scene but he just kind of slides around with no animation, but now I have finally got around to animation.
 
Because my character is made of multiple sprites with their own order in layer, he flickers and z fights constantly in game mode, I worked out that if I uncheck offset original order and affect children he now appears in game displayed correctly and he orders are not broken/overwritten.

But the things in the scene that he used to walk in front of or behind now ignore where he is on the sorting map, no matter what order in layer I set things to I can't seem to get it set up correctly again, as it was when he was a single sprite at a time.

Is this because he has so many order layers but the sorting map treats him as one compared to the rest of the scene?

I tried to fix this by changing the sorting map to change the players sorting layer instead but it ignores them too, I can go into game mode and see the player dynamically changing sorting layers as I move around but things that he should walk in front of that are definitely set to a lower sorting layer, he still walks behind them.

This is driving me crazy as I was really starting to get somewhere :(

I hope that made sense and someone can help me out

Comments

  • On further investigation, this sounds like exactly what I need: 
    http://adventure-creator.wikia.com/wiki/Special:Search?query=mechanim+sorting+maps
    Except I have no idea how to make a mechanim version of this script :/

    Edit:
    I managed to get it working(ish) without even knowing what i am doing! I am allowed back into game without the 'all compiler errors must be fixed' message,
    but it only affects the top sprite of the characters heirarchy and not the children, so close!! :P 

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;





    public class CopySpriteOrder : MonoBehaviour

    {



    [SerializeField] private SpriteRenderer spriteRendererToUpdate;

    [SerializeField] private SpriteRenderer spriteRendererToCopy;



    void Update ()

    {
    Transform[] allChildren = GetComponentsInChildren<Transform>();
    foreach (Transform child in allChildren) 

    spriteRendererToUpdate.sortingLayerName = spriteRendererToCopy.sortingLayerName;

    spriteRendererToUpdate.sortingOrder = spriteRendererToCopy.sortingOrder;

    }



    }

    Hopefully someone can see any obvious mistake? 
    Thanks in advance :)
  • I think you're going to have to elaborate some more on how exactly your characters are animated.  2D sprites with Mecanim animation isn't common, so I don't know what it is exactly you're trying to achieve.  Sprites Unity or Sprites Unity Complex animation engines are recommended for sprite-based characters.

    The two options you unchecked in your Follow Sorting Map are there to solve the apparent issue you're having.  Assuming the root sprite is zero, then all other child sprites retain their relative offset to this.

    Eg, if the state when in Edit mode is:
    (root) Body sprite = 0, Head sprite = 1, Shadow sprite = -1

    Then when the character moves into a Sorting Map region = 5 at runtime:
    (root) Body sprite = 5, Head sprite = 6, Shadow sprite = 4

    The included 2D Demo player prefab, Brain2D, has this for his own shadow sprite.  What exactly was going on with the character when these options were checked?

    Regarding your own script: you haven't explained what it is exactly you're trying to achieve with it, but you don't refer to the "child" Transforms you're iterating through.  Did you mean something more like:

    foreach (Transform child in allChildren)
    {
        child.GetComponent <SpriteRenderer>().sortingLayerName = spriteRendererToCopy.sortingLayerName;
        child.GetComponent <SpriteRenderer>().sortingOrder = spriteRendererToCopy.sortingOrder;
    }

  • Hi Chris thanks for the reply,
    Sorry I didn't mean to say mechanim, my player uses Sprites Unity.

    I have a character made up of multiple sprites linked up in a hierarchy starting from the body sprite,  and animated in unity's animation window.
    Because he has multiple orders in layer he flickered and z fought constantly when in game mode until I unchecked Offset original Order and Also affect children.

    With those unchecked he maintained the order within himself but no longer worked in the scenes sorting map.

    I read the post by you about a solution someone needed when using a character from Anima 2D, you explained that he needed to add a blank sprite renderer and a script to copy from that one to the characters sprite renderer.
    When I used this (had to adjust it to work without the Anima 2D specific references, I'm amazed it even worked at all!) it worked on only the top of the hierarchy, the body sprite, but all the children sprites, arms and legs etc, still appeared out of order in the sorting map, so if I could get this script to also affect the children of the body it would work perfectly.
    Hope that makes sense
  • Weird, it's suddenly working! :/ the only change I made is changing the sorting map to be sorting layers instead and rechecked Offset original Order and Also affect children, I disabled the CopySpriteOrder script completely and it suddenly worked,
    like I said a barely know what I am doing so it's amazing anything is working at all!
    Thanks Chris, hopefully I can make some decent progress now :) 
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.