using UnityEngine; namespace CreatureControl { /// /// A target rig or skeleton for a creature /// public class TargetRig : MonoBehaviour { /// /// Pose the target rig using the IK targets /// public virtual void Pose() { } /// /// Align the target rig with a creature /// /// The creature to align to public void MatchTo(Creature creature) { bool anythingChangedDummy = false; MatchTo(creature, ref anythingChangedDummy); } /// /// Align the target rig with a creature /// /// The creature to align to /// True when any property of the creature has changed public virtual void MatchTo(Creature creature, ref bool anythingChanged) { Vector3 targetToModelTranslation = creature.model.position - this.transform.position; bool changed = targetToModelTranslation != creature.targetToModelTranslation; if (changed) { anythingChanged = true; creature.targetToModelTranslation = targetToModelTranslation; } Quaternion targetToModelRotation = Quaternion.Inverse(this.transform.rotation) * creature.model.rotation; changed = targetToModelRotation != creature.targetToModelRotation; if (changed) { anythingChanged = true; creature.targetToModelRotation = targetToModelRotation; } } /// /// Compute the rotation from the target bone to a creature's bone /// /// The creature's bone for this target bone /// The next bone in the creature's hierarchy /// The rotation from the target bone rotation to the creature bone rotation /// The next bone is used to compute the direction of the bone. /// The 'up'-direction of the bone is currently fixed to (world) up. public static Quaternion TargetToBoneRotation(Transform bone, Transform nextBone) { if (bone == null || nextBone == null) return Quaternion.identity; Vector3 direction = nextBone.position - bone.position; Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up); Quaternion toBoneRotation = Quaternion.Inverse(targetRotation) * bone.rotation; return toBoneRotation; } } }