using UnityEngine; namespace Passer.CreatureControl { public class Creature : MonoBehaviour { /// The target bones rig /// The target bones rig contain the target pose of the creature /// The creature movements will try to move the creature such that the target pose is reached /// as closely as possible public TargetRig targetRig; public Transform creatureRig; public Vector3 targetToRootTranslation; public Quaternion targetToRootRotation; /// /// Ensure a target rig is available /// /// The name of the target rig resource /// True when the creature rig has been updated /// The parameter is used to instantiate a new target rig when none has been found. public bool CheckTargetRig(string targetRigResourceName) { if (this.targetRig == null) { // See if there is a target rig, but we just haven't found it this.targetRig = this.GetComponentInChildren(); if (this.targetRig == null) { GameObject targetsRigPrefab = Resources.Load(targetRigResourceName); GameObject targetRig = Instantiate(targetsRigPrefab); targetRig.name = "Target Rig"; targetRig.transform.SetPositionAndRotation(this.transform.position, this.transform.rotation); targetRig.transform.SetParent(this.transform); this.targetRig = targetRig.GetComponent(); } return true; } else return false; } /// /// Ensure that the creature rig is available /// /// True when the creature rig has been updated public bool CheckCreatureRig() { if (this.creatureRig == null) { SkinnedMeshRenderer[] skinnedMeshRenderers = this.GetComponentsInChildren(); foreach (SkinnedMeshRenderer skinnedMeshRenderer in skinnedMeshRenderers) { Transform rendererParent = skinnedMeshRenderer.transform.parent; if (this.creatureRig == null || this.creatureRig == rendererParent) this.creatureRig = rendererParent; else Debug.LogWarning("Unclear avatar root"); } return this.creatureRig != null; } else return false; } public virtual void UpdateBones() { Vector3 newPosition = this.targetRig.transform.position + targetToRootTranslation; Quaternion newOrientation = this.targetRig.transform.rotation * targetToRootRotation; this.transform.SetPositionAndRotation(newPosition, newOrientation); } } }