using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using SensorToolkit; using NodeCanvas.Framework; using RootMotion.Dynamics; using RootMotion.FinalIK; namespace KairoEngine.CharacterSystem { [System.Serializable] public class SkinRig { public string name = "Rig 1"; public Animator animator; public GameObject rootBone; public SkinnedMeshRenderer meshRenderer; public TriggerSensor visionSensor; public RangeSensor hearingSensor; public GameObject head; public GameObject rightHand; public GameObject leftHand; public GameObject rightUnarmedObject; public GameObject leftUnarmedObject; public List boneTargets = new List(); public GameObject puppetMasterObject; } public class SkinRigger : MonoBehaviour { public CharacterController character; public List skinRigs = new List(); public void Setup(ModelData modelData, Material material) { SkinRig skinRig = null; foreach (var item in skinRigs) { item.animator.gameObject.SetActive(false); item.puppetMasterObject.SetActive(false); if(modelData.skinRig == item.name) skinRig = item; } character.animator = skinRig.animator; character.meshRenderer = skinRig.meshRenderer; character.visionSensor = skinRig.visionSensor; character.hearingSensor = skinRig.hearingSensor; character.head = skinRig.head; character.rightHand = skinRig.rightHand; character.leftHand = skinRig.leftHand; character.rightUnarmedObject = skinRig.rightUnarmedObject; character.leftUnarmedObject = skinRig.leftUnarmedObject; Blackboard blackboard = character.GetComponent(); blackboard.SetVariableValue("visionSensor", character.visionSensor); blackboard.SetVariableValue("soundSensor", character.hearingSensor); skinRig.animator.gameObject.SetActive(true); skinRig.rootBone.SetActive(true); skinRig.meshRenderer.gameObject.SetActive(true); Mesh mesh = new Mesh(); mesh = modelData.mesh; character.meshRenderer.sharedMesh = modelData.mesh; character.meshRenderer.BakeMesh(mesh); character.meshRenderer.material = material; character.animator.enabled = true; character.animator.Rebind(); // Set LOS Targets LOSTargets losTargets = GetComponent(); if(losTargets != null) { for (int i = 0; i < losTargets.Targets.Length; i++) { losTargets.Targets[i] = skinRig.boneTargets[i]; } } skinRig.puppetMasterObject.SetActive(true); character.puppetMaster = skinRig.puppetMasterObject.GetComponent(); AimIK aimIk = character.GetComponentInChildren(); if(aimIk != null) { aimIk.solver.bones[0].transform = GetBone("Hips", skinRig.rootBone.transform); aimIk.solver.bones[1].transform = GetBone("Spine_01", skinRig.rootBone.transform); aimIk.solver.bones[2].transform = GetBone("Spine_02", skinRig.rootBone.transform); aimIk.solver.bones[3].transform = GetBone("Spine_03", skinRig.rootBone.transform); } LimbIK limbIk = character.GetComponentInChildren(); if(limbIk != null) { limbIk.solver.bone1.transform = GetBone("Shoulder_L", skinRig.rootBone.transform); limbIk.solver.bone2.transform = GetBone("Elbow_L", skinRig.rootBone.transform); limbIk.solver.bone3.transform = GetBone("Hand_L", skinRig.rootBone.transform); } } private Transform GetBone(string name, Transform root) { Transform[] bones = root.GetComponentsInChildren(); for (int i = 0; i < bones.Length; i++) { if(bones[i].name == name) return bones[i]; } return null; } } }