65 lines
2.8 KiB
C#
65 lines
2.8 KiB
C#
using UnityEngine;
|
|
|
|
namespace Passer.CreatureControl {
|
|
|
|
/// <summary>
|
|
/// A target rig for a creature
|
|
/// </summary>
|
|
public class TargetRig : MonoBehaviour {
|
|
|
|
public Animator animator;
|
|
|
|
public virtual void PoseLimbs() {
|
|
}
|
|
|
|
public void MatchTo(Creature creature) {
|
|
bool anythingChangedDummy = false;
|
|
MatchTo(creature, ref anythingChangedDummy);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public static Quaternion TargetToBoneRotation(Transform target, Transform nextTarget, Transform bone) {
|
|
Vector3 direction = nextTarget.position - bone.position;
|
|
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
|
|
Quaternion toBoneRotation = Quaternion.Inverse(targetRotation) * bone.rotation;
|
|
return toBoneRotation;
|
|
}
|
|
|
|
public static Quaternion TargetToBoneRotation_Debug(Transform target, Transform nextTarget, Transform bone) {
|
|
Vector3 direction = nextTarget.position - target.position;
|
|
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
|
|
Debug.DrawRay(bone.position, targetRotation * Vector3.forward, Color.blue);
|
|
Debug.DrawRay(bone.position, targetRotation * Vector3.up, Color.green);
|
|
|
|
Quaternion toBoneRotation = Quaternion.Inverse(targetRotation) * bone.rotation;
|
|
Debug.Log($"{targetRotation.eulerAngles} {bone.rotation.eulerAngles} -> {toBoneRotation.eulerAngles}");
|
|
return toBoneRotation;
|
|
}
|
|
}
|
|
|
|
} |