Equipment stretching randomly?

So I’m not sure why this is happening.



I don’t even know what caused it either. It just randomly started happening. It used to work perfectly, then all of the sudden, this monstrosity.

What happens in this project is when I equip an item on the chest or the head it gets severely warped. I can’t find any reason for this warping. It doesn’t happen when I equip any other items, or weapons, which are all using the same script. It is a bit of a weird one for me.

I tried some debugging and narrowing down the issue. I am fairly certain it has nothing to do with the model itself, or exporting the model. I tried deleting all my models and importing them again. So I know its not that. It appears to be after I instantiate the model and try to stitch it to the mesh that it is having problems. I am not sure why, everything is in place and its all using the same code. Here is the stitching code:

public class Stitcher
{
    /// <summary>
    /// Stitch clothing onto an avatar.  Both clothing and avatar must be instantiated however clothing may be destroyed after.
    /// </summary>
    /// <param name="sourceClothing"></param>
    /// <param name="targetAvatar"></param>
    /// <returns>Newly created clothing on avatar</returns>
    public GameObject Stitch(GameObject sourceClothing, GameObject targetAvatar)
    {
        var boneCatalog = new TransformCatalog(targetAvatar.transform);
        var skinnedMeshRenderers = sourceClothing.GetComponentsInChildren<SkinnedMeshRenderer>();
        var targetClothing = AddChild(sourceClothing, targetAvatar.transform);


        foreach (var sourceRenderer in skinnedMeshRenderers)
        {
            var targetRenderer = AddSkinnedMeshRenderer(sourceRenderer, targetClothing);
            targetRenderer.bones = TranslateTransforms(sourceRenderer.bones, boneCatalog);
        }
        return targetClothing;
    }

    private GameObject AddChild(GameObject source, Transform parent)
    {
        source.transform.parent = parent;

        foreach (Transform child in source.transform)
        {
            Object.Destroy(child.gameObject);
        }

        return source;
    }

    private SkinnedMeshRenderer AddSkinnedMeshRenderer(SkinnedMeshRenderer source, GameObject parent)
    {
        GameObject meshObject = new GameObject(source.name);
        meshObject.transform.parent = parent.transform;

        var target = meshObject.AddComponent<SkinnedMeshRenderer>();
        target.sharedMesh = source.sharedMesh;
        target.materials = source.materials;
        return target;
    }

    private Transform[] TranslateTransforms(Transform[] sources, TransformCatalog transformCatalog)
    {
        var targets = new Transform[sources.Length];
        for (var index = 0; index < sources.Length; index++)
            targets[index] = DictionaryExtensions.Find(transformCatalog, sources[index].name);
        return targets;
    }

    #region TransformCatalog
    private class TransformCatalog : Dictionary<string, Transform>
    {
        #region Constructors
        public TransformCatalog(Transform transform)
        {
            Catalog(transform);
        }
        #endregion

        #region Catalog
        private void Catalog(Transform transform)
        {
            if (ContainsKey(transform.name))
            {
                Remove(transform.name);
                Add(transform.name, transform);
            }
            else
                Add(transform.name, transform);
            foreach (Transform child in transform)
                Catalog(child);
        }
        #endregion
    }
    #endregion


    #region DictionaryExtensions
    private class DictionaryExtensions
    {
        public static TValue Find<TKey, TValue>(Dictionary<TKey, TValue> source, TKey key)
        {
            TValue value;
            source.TryGetValue(key, out value);
            return value;
        }
    }
    #endregion

}

I don’t think this is the issue, but I can’t find anywhere else where there could be an issue. The thing is, this is the same code used for the other equipment parts, so if it works for them then it should work for the 2 areas that are messing up.

Any help?

Interesting, well I’m not experienced enough in this to explain to you how to fix it but what I can tell you right away is that the model is stretching directly towards the anchor point of your inventory UI. If it only happens when you equip your armour then it’s entirely possible the model is parenting itself or some other part of the code is causing it to stretch to the inventory slot, that’s the only reason I can think of for this stretching behaviour.

You aren’t using the exact same model or a similarly named prefab on the human and putting it into the inventory are you? They need to be seperate prefabs, if they’re all the same then that’s what is likely causing weird issues, I noticed you’re using .name in your code which will cause problems if you’re not careful.

Thanks for the reply! Honestly even a little help is going a long way on this one.

It could be the .name that is throwing everything off. The script is meant to add a mesh to the player mesh, so if its grabbing thw wrong reference (because I mistakenly named both the model and UI object the exact same) it might be stitching between the two gameobjects.

I’ll try renamimg the files and see if that fixes things. Any suggestions on a naming convention for models? Thanks so much for the help!

Hm, I’m not so sure the name is the issue. I tried naming everything differently but it didn’t seem to change anything. I still have no idea why the origin of the UI would be the problem

Found the issue. I was grabbing my entire player to set as the Avatar variable, when I should have been grabbing just the player model.

Appreciate the help!

I had no idea how to help you, but saw your threads. I’m glad you worked it out. :slight_smile: