Forum rules - please read before posting.

Sprite Child local x,y position reset to zero when another character's in the scene

edited January 2018 in Engine development
AC Version 1.60.5 - Unity 2017.1.0f3
The graphics of my sprites have a vertical offset - they aren't exactly touching the bottom of their bounds (for various reasons). So what I did is lowering the Sprite Child transform so that the point where the feet are touching the ground corresponds to the pivot of the Player-NPC gameobject so that the position of the sprite is more accurate. I of course get the warning "The sprite child of <name> is not positioned at (0, 0, 0) - is this correct?" which is indeed correct in this case.
It worked fine, until I added a second character (an NPC) with the same issue in the scene. When I pressed play, all the characters have their sprite child local x,y position reset to zero. If I remove the second character from the scene. It doesn't happen.

So wondering if there was a reason for that - and for thinking for another solution to fix that vertical offset, I looked around the AC scripts to better understand. And in 'FollowSortingMap.cs' within function 'protected void SetOriginalDepth' at line 194, I saw this :
originalDepth = transform.parent.position - transform.parent.position;
I personally thought that was weird and changed it into:
originalDepth = transform.position - transform.parent.position;
which fixed my issue. However I don't know if it was a voluntary choice for something I didn't catch, or a simple typo.

So I'm posting it here to pinpoint this issue. I don't know why it only happens when more than one character is in the scene.

On a related note, later in the same script within function "public void SetDepth" at line 226, I also changed the line:
transform.position = transform.parent.position + originalDepth + (transform.forward * depth);
into:
transform.position = new Vector3(transform.x, transform.y, transform.parent.position.z + originalDepth.z + (transform.forward.z * depth));
to allow changing the vertical position for another use, like floating characters.

I'm really happy with Adventure Creator. Good job for everyone involved!

Comments

  • Welcome to the community, @Yeppoh.

    This is all due to the way AC deals with two 2D characters within the same region of a Sorting Map.  When in the same region, their sorting orders are both identical - so AC corrects their display order by moving their sprite childs every-so-slightly in the Z direction.

    Thanks for the code suggestions, I'll review them - however, the way I recommend getting aroung the (0,0,0) local position requirement is to make the sprite itself a child object of the "Sprite child" transform.  This transform doesn't need to have the sprite itself, just within its own hierarchy.  Is that possible with your setup?
  • Thank you very much!

    My surprise with the first code suggestion was the parent transform position subtracting by itself, as if there was some sort of supernatural coding trick behind.

    Your recommendation should be possible with my setup.
    To be sure I got that right :
    - I create an empty transform that is passed as the "Sprite Child" in the NPC/Player component in the parent
    - and then put the transform containing the Sprite Renderer, Follow Sorting Map, and Animator components as a child to that empty "Sprite Child" transform.
    Is that it?
  • edited January 2018
    That's correct, save for the Animator.

    Are you using the "Sprites Unity" animation engine?  The Animator component is expected to be directly on the sprite child or the root object - but I will expose a field in the Player/NPC component to allow for custom placement in the next update, v1.61.
  • Sprite Unity, yes.
    So, Animator stays on the "Sprite Child".

    Thank you!
  • Not necessary - like I said, I'll make it optional in the upcoming v1.61.

    In the meantime, you can assign it by temporarily switching the animation engine to "Mecanim", assigning the Animator component (not on the sprite child) in the newly-exposed "Custom animator" field, and then switching back to "Sprites Unity".
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.