public static void MaskOutputRotations(Transform xform, UnityConstraints.OUTPUT_ROT_OPTIONS option) { switch (option) { case UnityConstraints.OUTPUT_ROT_OPTIONS.WorldX: { Vector3 vector = xform.eulerAngles; vector.y = 0f; vector.z = 0f; xform.eulerAngles = vector; break; } case UnityConstraints.OUTPUT_ROT_OPTIONS.WorldY: { Vector3 vector = xform.eulerAngles; vector.x = 0f; vector.z = 0f; xform.eulerAngles = vector; break; } case UnityConstraints.OUTPUT_ROT_OPTIONS.WorldZ: { Vector3 vector = xform.eulerAngles; vector.x = 0f; vector.y = 0f; xform.eulerAngles = vector; break; } case UnityConstraints.OUTPUT_ROT_OPTIONS.LocalX: { Vector3 vector = xform.localEulerAngles; vector.y = 0f; vector.z = 0f; xform.localEulerAngles = vector; break; } case UnityConstraints.OUTPUT_ROT_OPTIONS.LocalY: { Vector3 vector = xform.localEulerAngles; vector.x = 0f; vector.z = 0f; xform.localEulerAngles = vector; break; } case UnityConstraints.OUTPUT_ROT_OPTIONS.LocalZ: { Vector3 vector = xform.localEulerAngles; vector.x = 0f; vector.y = 0f; xform.localEulerAngles = vector; break; } } }
/// <summary> /// Runs when the constraint is active or when the noTarget mode is set to /// ReturnToDefault /// </summary> private void OutputTowards(Quaternion destRot) { UnityConstraints.InterpolateRotationTo ( this.xform, destRot, this.interpolation, this.speed ); UnityConstraints.MaskOutputRotations(this.xform, this.output); }
/// <summary> /// Runs when the constraint is active or when the noTarget mode is set to /// ReturnToDefault /// </summary> private void OutputRotationTowards(Quaternion destRot) { // Faster exit if nothing to do. if (!this.constrainRotation) { return; } UnityConstraints.InterpolateRotationTo ( this.xform, destRot, this.interpolation, this.rotationSpeed ); UnityConstraints.MaskOutputRotations(this.xform, this.output); }
public static void InterpolateRotationTo(Transform xform, Quaternion targetRot, UnityConstraints.INTERP_OPTIONS interpolation, float speed) { Quaternion rotation = xform.rotation; Quaternion rotation2 = Quaternion.identity; float deltaTime = Time.deltaTime; switch (interpolation) { case UnityConstraints.INTERP_OPTIONS.Linear: rotation2 = Quaternion.Lerp(rotation, targetRot, deltaTime * speed); break; case UnityConstraints.INTERP_OPTIONS.Spherical: rotation2 = Quaternion.Slerp(rotation, targetRot, deltaTime * speed); break; case UnityConstraints.INTERP_OPTIONS.SphericalLimited: rotation2 = Quaternion.RotateTowards(rotation, targetRot, speed * Time.timeScale); break; } xform.rotation = rotation2; }
/// <summary> /// Runs each frame while the constraint is active /// </summary> protected override void OnConstraintUpdate() { // Note: Do not run base.OnConstraintUpdate. It is not implimented if (this.constrainScale) { this.SetWorldScale(target); } if (this.constrainRotation) { this.xform.rotation = this.target.rotation; UnityConstraints.MaskOutputRotations(this.xform, this.output); } if (this.constrainPosition) { this.pos = this.xform.position; // Output only if wanted if (this.outputPosX) { this.pos.x = this.target.position.x; } if (this.outputPosY) { this.pos.y = this.target.position.y; } if (this.outputPosZ) { this.pos.z = this.target.position.z; } this.xform.position = pos; } }