74 lines
3.3 KiB
C#

using UnityEngine;
namespace Passer.CreatureControl {
// An insect target rig....
public class TargetRig : MonoBehaviour {
public TargetLeg leftFrontLeg;
public TargetLeg leftMiddleLeg;
public TargetLeg leftBackLeg;
public TargetLeg rightFrontLeg;
public TargetLeg rightMiddleLeg;
public TargetLeg rightBackLeg;
public Animator animator;
public void PoseLimbs() {
this.leftBackLeg.PoseLimb();
this.leftMiddleLeg.PoseLimb();
this.leftFrontLeg.PoseLimb();
this.rightBackLeg.PoseLimb();
this.rightMiddleLeg.PoseLimb();
this.rightFrontLeg.PoseLimb();
}
public void MatchTo(Insect insect) {
if (insect == null)
return;
if (this.leftFrontLeg != null && insect.leftFrontLeg != null)
this.leftFrontLeg.MatchTo(insect.leftFrontLeg);
if (this.leftMiddleLeg != null && insect.leftMiddleLeg != null)
this.leftMiddleLeg.MatchTo(insect.leftMiddleLeg);
if (this.leftBackLeg != null && insect.leftHindLeg != null)
this.leftBackLeg.MatchTo(insect.leftHindLeg);
if (this.rightFrontLeg != null && insect.rightFrontLeg != null)
this.rightFrontLeg.MatchTo(insect.rightFrontLeg);
if (this.rightMiddleLeg != null && insect.rightMiddleLeg != null)
this.rightMiddleLeg.MatchTo(insect.rightMiddleLeg);
if (this.rightBackLeg != null && insect.rightHindLeg != null)
this.rightBackLeg.MatchTo(insect.rightHindLeg);
}
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;
}
}
}