最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unity game engine - How do you get a prefab w Skinned Mesh Renderer to attach to an avatar on instantiation? - Stack Overflow

programmeradmin0浏览0评论

I am working on a modular equipment system. The player will be able to swap out many pieces of gear and see it change on their model. I ultimately want to load the gear pieces through the addressables system. The problem I have is that the modular prefab pieces lose reference to the players rig (as you would expect since it’s in the scene) and stop rendering properly as a result.

I have tried creating a reference to all the bones for the prefab’s SkinMeshRenderer (via C#) but it doesn’t work properly. Here’s what I see. The left is the instantiated prefab as described above. The right is before I delete the newly created prefab from the scene, and thus it hasn’t lost the bones:

Here’s the code that produced this:

void AssignArmorToArmature()
{
    // Instantiate the armor prefab
    GameObject armorInstance = Instantiate(armorPrefab, Vector3.zero, Quaternion.identity, playerArmature.parent.parent);
    armorInstance.transform.localPosition = Vector3.zero;
    armorInstance.transform.localRotation = Quaternion.identity;

    // Get the SkinnedMeshRenderer from the armor instance
    SkinnedMeshRenderer armorRenderer = armorInstance.GetComponentInChildren<SkinnedMeshRenderer>();

    armorRenderer.bones = playerArmature.GetComponentsInChildren<Transform>();
    armorRenderer.rootBone = playerArmature; 
}

I know that the model is weight painted correctly and the bones are named the same, so that doesn’t appear to be the issue. What else can I try?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论