private void DrawConstraint(BoneConstraint constraint) { if (constraint.BoneToConstrain == null || constraint.BoneToConstrain.parent == null) { return; } Gizmos.matrix = constraint.BoneToConstrain.parent.localToWorldMatrix; // draw line to minAngle Gizmos.color = Color.green; Vector3 minPointRotated = Quaternion.AngleAxis(constraint.StartAngle, Vector3.forward) * Vector3.right; Gizmos.DrawLine(constraint.BoneToConstrain.localPosition, constraint.BoneToConstrain.localPosition + minPointRotated * 0.01f); // draw line to maxAngle Gizmos.color = Color.blue; Vector3 maxPointRotated = Quaternion.AngleAxis(constraint.EndAngle, Vector3.forward) * Vector3.right; Gizmos.DrawLine(constraint.BoneToConstrain.localPosition, constraint.BoneToConstrain.localPosition + maxPointRotated * 0.01f); Gizmos.matrix = Matrix4x4.identity; Gizmos.color = Color.red; Gizmos.DrawLine(constraint.BoneToConstrain.position, constraint.BoneToConstrain.position + constraint.BoneToConstrain.right * 0.015f); }
private void SetTransformWithConstraint(BoneConstraint constraint, Transform bone, Vector3 position, Quaternion globalRotation) { constraint.BoneToConstrain.position = position; constraint.BoneToConstrain.localPosition = new Vector3(constraint.BoneToConstrain.localPosition.x, constraint.YHeightCorrection, 0); Quaternion localRotation = InverseTransformQuaternion(bone.transform.parent, globalRotation); Vector3 localEuler = localRotation.eulerAngles; float startAngle = constraint.StartAngle, endAngle = constraint.EndAngle; float maxAngle, minAngle; maxAngle = (startAngle > endAngle) ? startAngle : endAngle; minAngle = (startAngle > endAngle) ? endAngle : startAngle; float distToMax = distToMax = Mathf.DeltaAngle(localEuler.z, maxAngle); float distToMin = distToMin = Mathf.DeltaAngle(localEuler.z, minAngle); if (localEuler.z > maxAngle || localEuler.z < minAngle) { // move euler.z to closest angle localEuler.z = (Mathf.Abs(distToMax) < Mathf.Abs(distToMin)) ? maxAngle : minAngle; } constraint.BoneToConstrain.localRotation = Quaternion.Euler(new Vector3(0, 0, localEuler.z)); }
BoneConstraint First(int instanceID) { for (int i = 0; i < boneConstraints.Length; i++) { BoneConstraint constraint = boneConstraints[i]; if (constraint.BoneToConstrain.GetInstanceID() == instanceID) { return(constraint); } } return(new BoneConstraint()); }
bool AnyBoneConstraint(int instanceID) { for (int i = 0; i < boneConstraints.Length; i++) { BoneConstraint constraint = boneConstraints[i]; if (constraint.BoneToConstrain.GetInstanceID() == instanceID) { return(true); } } return(false); }
public void SetTransformWithConstraint(Transform bone, Vector3 position, Quaternion globalRotation) { if (AnyBoneConstraint(bone.GetInstanceID())) { // get our constraint object BoneConstraint constraint = First(bone.GetInstanceID()); SetTransformWithConstraint(constraint, bone, position, globalRotation); } else { // just apply without constraint bone.transform.SetPositionAndRotation(position, globalRotation); } }