99 lines
3.6 KiB
C#
99 lines
3.6 KiB
C#
using UnityEngine;
|
|
|
|
namespace Passer.CreatureControl {
|
|
|
|
public class Creature : MonoBehaviour {
|
|
/// <summary>The target bones rig</summary>
|
|
/// 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 Transform model;
|
|
|
|
public TargetRig targetRig;
|
|
|
|
public Vector3 targetToModelTranslation;
|
|
public Quaternion targetToModelRotation;
|
|
|
|
#region Init
|
|
|
|
/// <summary>
|
|
/// Ensure a target rig is available
|
|
/// </summary>
|
|
/// <param name="targetRigResourceName">The name of the target rig resource</param>
|
|
/// <returns>True when the creature rig has been updated</returns>
|
|
/// 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<InsectRig>();
|
|
if (this.targetRig == null) {
|
|
GameObject targetsRigPrefab = Resources.Load<GameObject>(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<InsectRig>();
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public virtual bool CheckTargetRig() {
|
|
return CheckTargetRig("TargetRig");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensure that the creature rig is available
|
|
/// </summary>
|
|
/// <returns>True when the creature rig has been updated</returns>
|
|
public bool CheckModel() {
|
|
if (this.model == null) {
|
|
SkinnedMeshRenderer[] skinnedMeshRenderers = this.GetComponentsInChildren<SkinnedMeshRenderer>();
|
|
foreach (SkinnedMeshRenderer skinnedMeshRenderer in skinnedMeshRenderers) {
|
|
Transform rendererParent = skinnedMeshRenderer.transform.parent;
|
|
if (this.model == null || this.model == rendererParent)
|
|
this.model = rendererParent;
|
|
else
|
|
Debug.LogWarning("Unclear model root");
|
|
}
|
|
return this.model != null;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
#endregion Init
|
|
|
|
#region Start
|
|
|
|
protected virtual void Start() {
|
|
this.CheckTargetRig();
|
|
this.CheckModel();
|
|
this.targetRig.MatchTo(this);
|
|
}
|
|
|
|
#endregion Start
|
|
|
|
#region Update
|
|
|
|
public virtual void Update() {
|
|
if (this.targetRig == null)
|
|
return;
|
|
|
|
this.targetRig.PoseLimbs();
|
|
UpdateBones();
|
|
}
|
|
|
|
public virtual void UpdateBones() {
|
|
Vector3 newPosition = this.targetRig.transform.position + this.targetToModelTranslation;
|
|
Quaternion newOrientation = this.targetRig.transform.rotation * this.targetToModelRotation;
|
|
this.model.SetPositionAndRotation(newPosition, newOrientation);
|
|
}
|
|
|
|
#endregion Update
|
|
}
|
|
|
|
} |