/// <summary> /// Performs a squash and stretch animation, while changing from a target scale. /// </summary> /// <param name="scale">The value to animate.</param> /// <param name="startScale">The scale to animate from.</param> /// <param name="amplitude">The amplitude of a squash and strech</param> /// <param name="duration">The duration of the animation</param> /// <param name="normal"> The normal of the animation. </param> /// <param name="tangent"> The tangent of the animation. </param> public static CommandDelegate ScaleSquashAndStretchFrom(Ref <Vector3> scale, Vector3 startScale, float amplitude, double duration, Vector3 normal, Vector3 tangent) { CheckArgumentNonNull(scale, "scale"); Vector3 targetScale = Vector3.zero; return(Cmd.Sequence( Cmd.Do(() => { targetScale = scale.Value; scale.Value = startScale; }), Cmd.Defer(() => Cmd.ScaleSquashAndStretchTo(scale, targetScale, amplitude, duration, normal, tangent)) )); }
/// <summary> /// Shakes the vector several times in a random value, up to amount in magnitude. /// </summary> /// <param name="amount">The maximum displacement of the rotation, in degrees.</param> /// <param name="numShakes"> The number of shakes to perform. </param> public static CommandDelegate Shake(Ref <Quaternion> rotation, float amount, int numShakes, double duration) { CheckArgumentNonNull(rotation); if (amount <= 0f || amount > 180f) { throw new ArgumentOutOfRangeException("amount", "Must be larger than 0."); } if (numShakes <= 0) { throw new ArgumentOutOfRangeException("numBounces", "Must be larger than 0."); } amount = Mathf.Abs(amount); double avgDuration = duration / (numShakes + 1); CommandDelegate[] list = new CommandDelegate[numShakes + 1]; return(Cmd.Defer( () => { var baseVal = rotation.Value; for (int i = 0; i < numShakes; ++i) { // Generate an offset within the range -amount, amount var offset = Quaternion.Euler( UnityEngine.Random.Range(-amount, amount), UnityEngine.Random.Range(-amount, amount), UnityEngine.Random.Range(-amount, amount) ); float angle = Quaternion.Angle(Quaternion.identity, offset); // Clamp the offset if (angle > amount) { float t = UnityEngine.Random.Range(0f, angle / amount); offset = Quaternion.LerpUnclamped(Quaternion.identity, offset, t); } list[i] = Cmd.RotateTo(rotation, baseVal * offset, avgDuration); } list[numShakes] = Cmd.RotateTo(rotation, baseVal, avgDuration); return Cmd.Sequence(list); } )); }
/// <summary> /// Pulsates a value. /// </summary> /// <param name="amount">The amount to increase the value by.</param> public static CommandDelegate PulsateScale(Ref <float> scale, float amount, double duration) { CheckArgumentNonNull(scale, "scale"); CommandDelegate tweenBack = null; return(Cmd.Sequence( Cmd.Do(() => { // Because we don't know what the original scale is at this point, // we have to recreate the scale back tween every time. tweenBack = Cmd.ChangeTo(scale, scale.Value, duration / 2.0, Ease.Smooth()); }), Cmd.ChangeBy(scale, amount, duration / 2.0, Ease.Smooth()), Cmd.Defer(() => tweenBack) )); }
/// <summary> /// Shakes the vector several times in a random value, up to amount in magnitude. /// </summary> /// <param name="amount">The maximum displacement of the vector, with separate x,y and z values.</param> /// <param name="numShakes"> The number of shakes to perform. </param> public static CommandDelegate Shake(Ref <Vector3> vector, Vector3 amount, int numShakes, double duration) { CheckArgumentNonNull(vector); if (numShakes <= 0) { throw new ArgumentOutOfRangeException("numBounces", "Must be larger than 0."); } double avgDuration = duration / (numShakes + 1); CommandDelegate[] list = new CommandDelegate[numShakes + 1]; return(Cmd.Defer( () => { var baseVal = vector.Value; for (int i = 0; i < numShakes; ++i) { // Generate an offset within the range -1, 1 var offset = new Vector3( UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f) ); if (offset.magnitude > 1f) { // Randomize the length of the offset if it is too large. offset = offset.normalized * UnityEngine.Random.Range(0f, 1f); } // Scale the offset, by the amount, and the scale factor. offset = new Vector3(offset.x * amount.x, offset.y * amount.y, offset.z * amount.z); list[i] = Cmd.ChangeTo(vector, baseVal + offset, avgDuration); } list[numShakes] = Cmd.ChangeTo(vector, baseVal, avgDuration); return Cmd.Sequence(list); } )); }