1
\$\begingroup\$

I am currently working on a 2D game in Unity, I am using Animation Controllers / Animators for the different slots of armor on the character. The scripts set the Animation to the player object and that seems to work out alright for the most part.

However when I walk long distances without stopping it seems like the armor tends to drift away from the player object and I haven't been able to find out why.

// This will update the player and then update all his inventory to match his position.
public void UpdateMovement()
{
    if (IsActive == true)
    {
        float input_x = Input.GetAxisRaw("Horizontal");
        float input_y = Input.GetAxisRaw("Vertical");
        bool playerMoved = false;

        Vector2 movement = new Vector2(input_x, input_y);
        float delta = Time.deltaTime;

        if (movement != Vector2.zero)
        {
            _animator.SetBool("IsWalking", true);
            _animator.SetFloat("x", input_x);
            _animator.SetFloat("y", input_y);
            playerMoved = true;
        }
        else
        {
            _animator.SetBool("IsWalking", false);
            playerMoved = false;
        }


        PlayerController.transform.position += new Vector3(input_x, input_y, 0).normalized * delta;
        Inventory.UpdateMovement(input_x, input_y, delta, playerMoved, PlayerController.transform.position);
    }

   // This code is for each slot to update it's position.
   public void UpdateMovement(float x, float y, float time, bool isWalking, Vector3 pos)
    {
        _animator.SetBool("IsWalking", isWalking);

        if (isWalking == true)
        {

            _animator.SetFloat("x", x);
            _animator.SetFloat("y", y);
        }

        _animator.transform.position += new Vector3(x, y, 0).normalized * time;

        // _animator.transform.position = pos;
    }

So my question is, what is the best approach for layering armor sprite animations on a player in Unity 5?

\$\endgroup\$
  • \$\begingroup\$ You would typically set the child entities, literally as child gameobject to their parents in the scene hierarchy. That way, if you modify the parent transform, the child transforms are automatically updated. You can then modify the local transform details of the children if needed. \$\endgroup\$ – jgallant Jun 15 '16 at 15:22
1
\$\begingroup\$

Depending on how many potential pieces of armour a player can get throughout your game, it may be best to have a parent Game Object that contains both the player sprite and all of the armour they could ever have. Rather than moving the player sprite itself, you would then move the parent object (effectively this can be done with your player controller), and the armour will move with it.

All you need to do then is enable only the visible armour pieces. For exampole, your hierarchy could look like this:

- Player_Controller
   - Player_Sprite
   - Armour_Holder
       - Helmet_1
       - Helmet_2
       - Chest_plate_1
       - Chest_plate_2
       - Boots_1
       - Boots_2

For animation, you can either keep track of the enabled objects and their animators, or you can use a single animation controller with multiple layers and change the layer weight depending on what is being worn. If you don't want a seperate state machine for each layer, you can sync them

\$\endgroup\$
  • \$\begingroup\$ Awesome, thanks for the advice, I will try this when I get home. I am still pretty new to Unity so I will have to do some reading. \$\endgroup\$ – lakedoo Jun 15 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.