initial upload
This commit is contained in:
@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace RootMotion.FinalIK {
|
||||
|
||||
/// <summary>
|
||||
/// The base abstract class for all Transform constraints.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public abstract class Constraint {
|
||||
|
||||
#region Main Interface
|
||||
|
||||
/// <summary>
|
||||
/// The transform to constrain.
|
||||
/// </summary>
|
||||
public Transform transform;
|
||||
/// <summary>
|
||||
/// %Constraint weight.
|
||||
/// </summary>
|
||||
public float weight;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this <see cref="Constraint"/> is valid.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if is valid; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public bool isValid {
|
||||
get {
|
||||
return transform != null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the constraint.
|
||||
/// </summary>
|
||||
public abstract void UpdateConstraint();
|
||||
|
||||
#endregion Main Interface
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7e377232abb249ed8b4ef7d7ae933e7
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace RootMotion.FinalIK {
|
||||
|
||||
/// <summary>
|
||||
/// %Constraints to position in world space.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class ConstraintPosition : Constraint {
|
||||
|
||||
#region Main Interface
|
||||
|
||||
/// <summary>
|
||||
/// The target position.
|
||||
/// </summary>
|
||||
public Vector3 position;
|
||||
|
||||
public override void UpdateConstraint() {
|
||||
if (weight <= 0) return;
|
||||
if (!isValid) return;
|
||||
|
||||
// Lerping to position
|
||||
transform.position = Vector3.Lerp(transform.position, position, weight);
|
||||
}
|
||||
|
||||
#endregion Main Interface
|
||||
|
||||
public ConstraintPosition() {}
|
||||
public ConstraintPosition(Transform transform) {
|
||||
this.transform = transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 071793b8887c44ddda2d9dc3a0525167
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace RootMotion.FinalIK {
|
||||
|
||||
/// <summary>
|
||||
/// Offsets the transform from its (animated) position.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class ConstraintPositionOffset : Constraint {
|
||||
|
||||
#region Main Interface
|
||||
|
||||
/// <summary>
|
||||
/// The position offset in world space.
|
||||
/// </summary>
|
||||
public Vector3 offset;
|
||||
|
||||
public override void UpdateConstraint() {
|
||||
if (weight <= 0) return;
|
||||
if (!isValid) return;
|
||||
|
||||
// Initiating
|
||||
if (!initiated) {
|
||||
// Storing default values
|
||||
defaultLocalPosition = transform.localPosition;
|
||||
lastLocalPosition = transform.localPosition;
|
||||
|
||||
initiated = true;
|
||||
}
|
||||
|
||||
// Check if position has changed. If true, set default local position to current.
|
||||
if (positionChanged) defaultLocalPosition = transform.localPosition;
|
||||
|
||||
// Offsetting the position
|
||||
transform.localPosition = defaultLocalPosition;
|
||||
transform.position += offset * weight;
|
||||
|
||||
// Store the current local position to check if it has changed in the next update.
|
||||
lastLocalPosition = transform.localPosition;
|
||||
}
|
||||
|
||||
#endregion Main Interface
|
||||
|
||||
public ConstraintPositionOffset() {}
|
||||
|
||||
public ConstraintPositionOffset(Transform transform) {
|
||||
this.transform = transform;
|
||||
}
|
||||
|
||||
private Vector3 defaultLocalPosition, lastLocalPosition;
|
||||
private bool initiated;
|
||||
|
||||
/*
|
||||
* Check if position has been changed by animation or any other external script.
|
||||
* If not, consider the object to be static and offset only from the default rotation.
|
||||
* */
|
||||
private bool positionChanged {
|
||||
get {
|
||||
return transform.localPosition != lastLocalPosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2d9a3e673645474bb6716a1c403e3b1
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace RootMotion.FinalIK {
|
||||
|
||||
/// <summary>
|
||||
/// %Constraints to rotation in world space
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class ConstraintRotation : Constraint {
|
||||
|
||||
#region Main Interface
|
||||
|
||||
/// <summary>
|
||||
/// The target rotation.
|
||||
/// </summary>
|
||||
public Quaternion rotation;
|
||||
|
||||
public override void UpdateConstraint() {
|
||||
if (weight <= 0) return;
|
||||
if (!isValid) return;
|
||||
|
||||
// Slerping to target rotation
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, weight);
|
||||
}
|
||||
|
||||
#endregion Main Interface
|
||||
|
||||
public ConstraintRotation() {}
|
||||
public ConstraintRotation(Transform transform) {
|
||||
this.transform = transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a47eb259ef1b9403599c08fb1e457fcc
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace RootMotion.FinalIK {
|
||||
|
||||
/// <summary>
|
||||
/// Offsets the transform from its (animated) rotation
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class ConstraintRotationOffset: Constraint {
|
||||
|
||||
#region Main Interface
|
||||
|
||||
/// <summary>
|
||||
/// The rotation offset in world space.
|
||||
/// </summary>
|
||||
public Quaternion offset;
|
||||
|
||||
public override void UpdateConstraint() {
|
||||
if (weight <= 0) return;
|
||||
if (!isValid) return;
|
||||
|
||||
// Initiating
|
||||
if (!initiated) {
|
||||
// Storing default rotations.
|
||||
defaultLocalRotation = transform.localRotation;
|
||||
lastLocalRotation = transform.localRotation;
|
||||
|
||||
initiated = true;
|
||||
}
|
||||
|
||||
// Check if rotation has changed. If true, set default local rotation to current.
|
||||
if (rotationChanged) defaultLocalRotation = transform.localRotation;
|
||||
|
||||
// Offsetting the rotation
|
||||
transform.localRotation = defaultLocalRotation;
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, offset, weight);
|
||||
|
||||
// Store the current local rotation to check if it has changed in the next update.
|
||||
lastLocalRotation = transform.localRotation;
|
||||
}
|
||||
|
||||
#endregion Main Interface
|
||||
|
||||
public ConstraintRotationOffset() {}
|
||||
public ConstraintRotationOffset(Transform transform) {
|
||||
this.transform = transform;
|
||||
}
|
||||
|
||||
private Quaternion defaultRotation, defaultLocalRotation, lastLocalRotation, defaultTargetLocalRotation;
|
||||
private bool initiated;
|
||||
|
||||
/*
|
||||
* Check if rotation has been changed by animation or any other external script.
|
||||
* If not, consider the object to be static and offset only from the default rotation.
|
||||
* */
|
||||
private bool rotationChanged {
|
||||
get {
|
||||
return transform.localRotation != lastLocalRotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 530e73d7a55ff47029c43502e289273f
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,95 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace RootMotion.FinalIK {
|
||||
|
||||
/// <summary>
|
||||
/// Contains and manages a set of constraints.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class Constraints {
|
||||
|
||||
#region Main Interface
|
||||
|
||||
/// <summary>
|
||||
/// The transform.
|
||||
/// </summary>
|
||||
public Transform transform;
|
||||
/// <summary>
|
||||
/// The target.
|
||||
/// </summary>
|
||||
public Transform target;
|
||||
/// <summary>
|
||||
/// The position offset.
|
||||
/// </summary>
|
||||
public Vector3 positionOffset;
|
||||
/// <summary>
|
||||
/// The position to lerp to by positionWeight
|
||||
/// </summary>
|
||||
public Vector3 position;
|
||||
/// <summary>
|
||||
/// The weight of lerping to position
|
||||
/// </summary>
|
||||
[Range(0f, 1f)]
|
||||
public float positionWeight;
|
||||
/// <summary>
|
||||
/// The rotation offset.
|
||||
/// </summary>
|
||||
public Vector3 rotationOffset;
|
||||
/// <summary>
|
||||
/// The rotation to slerp to by rotationWeight
|
||||
/// </summary>
|
||||
public Vector3 rotation;
|
||||
/// <summary>
|
||||
/// The weight of slerping to rotation
|
||||
/// </summary>
|
||||
[Range(0f, 1f)]
|
||||
public float rotationWeight;
|
||||
|
||||
private Vector3 defaultLocalPosition;
|
||||
private Quaternion defaultLocalRotation;
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance is valid.
|
||||
/// </summary>
|
||||
public bool IsValid() {
|
||||
return transform != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initiate to the specified transform.
|
||||
/// </summary>
|
||||
public void Initiate(Transform transform) {
|
||||
this.transform = transform;
|
||||
this.position = transform.position;
|
||||
this.rotation = transform.eulerAngles;
|
||||
defaultLocalPosition = transform.localPosition;
|
||||
defaultLocalRotation = transform.localRotation;
|
||||
}
|
||||
|
||||
public void FixTransforms()
|
||||
{
|
||||
transform.localPosition = defaultLocalPosition;
|
||||
transform.localRotation = defaultLocalRotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the constraints.
|
||||
/// </summary>
|
||||
public void Update() {
|
||||
if (!IsValid()) return;
|
||||
|
||||
// Position
|
||||
if (target != null) position = target.position;
|
||||
transform.position += positionOffset;
|
||||
if (positionWeight > 0f) transform.position = Vector3.Lerp(transform.position, position, positionWeight);
|
||||
|
||||
// Rotation
|
||||
if (target != null) rotation = target.eulerAngles;
|
||||
transform.rotation = Quaternion.Euler(rotationOffset) * transform.rotation;
|
||||
if (rotationWeight > 0f) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotation), rotationWeight);
|
||||
}
|
||||
|
||||
#endregion Main Interface
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11679c68b0e4448338c1e21138a3596a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user