├── ProjectSettings ├── AudioManager.asset ├── InputManager.asset ├── TagManager.asset ├── TimeManager.asset ├── EditorSettings.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── DynamicsManager.asset ├── GraphicsSettings.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── QualitySettings.asset └── EditorBuildSettings.asset ├── .gitignore ├── Assets ├── AutoMotion.meta └── AutoMotion │ ├── Demo.meta │ ├── Demo │ ├── Materials │ │ ├── Colors │ │ │ ├── Blue.mat │ │ │ ├── Green.mat │ │ │ ├── Pink.mat │ │ │ ├── Red.mat │ │ │ ├── Purple.mat │ │ │ ├── Red.mat.meta │ │ │ ├── Blue.mat.meta │ │ │ ├── Green.mat.meta │ │ │ ├── Pink.mat.meta │ │ │ └── Purple.mat.meta │ │ └── Colors.meta │ ├── Scenes.meta │ ├── Scenes │ │ ├── AutoMotionDemo.unity │ │ └── AutoMotionDemo.unity.meta │ ├── Scripts.meta │ ├── Materials.meta │ └── Scripts │ │ ├── AutoMover.cs.meta │ │ ├── PathMover.cs.meta │ │ ├── PathMover.cs │ │ └── AutoMover.cs │ ├── Scripts.meta │ └── Scripts │ ├── Auto.cs.meta │ └── Auto.cs ├── README.md ├── AutoMotion.userprefs └── LICENSE /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | 5 | # Autogenerated VS/MD solution and project files 6 | *.csproj 7 | *.unityproj 8 | *.sln 9 | -------------------------------------------------------------------------------- /Assets/AutoMotion.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 57b22fb34970da04eb4f6fa6a84c00d5 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f02fac55f55dbfb4cb06e63c050f4bb2 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Blue.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/Assets/AutoMotion/Demo/Materials/Colors/Blue.mat -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Green.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/Assets/AutoMotion/Demo/Materials/Colors/Green.mat -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Pink.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/Assets/AutoMotion/Demo/Materials/Colors/Pink.mat -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Red.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/Assets/AutoMotion/Demo/Materials/Colors/Red.mat -------------------------------------------------------------------------------- /Assets/AutoMotion/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b2efa639aac04cf48b9cb3eff6bb748a 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Purple.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/Assets/AutoMotion/Demo/Materials/Colors/Purple.mat -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Red.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 49639be1d9055244d828836267cd13ec 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scenes.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2ba66e87f1dcba745ae205f9a609818b 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scenes/AutoMotionDemo.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/AutoMotion/HEAD/Assets/AutoMotion/Demo/Scenes/AutoMotionDemo.unity -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scenes/AutoMotionDemo.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f57edb5c46bbb4348ba35c7df0bed336 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a480af5baeced0d4b89ebff2316ef37d 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4c6cfff9da6890a439f1880a35ad41ab 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Blue.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0fb17c207e8b08f45b0381ebd0487947 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Green.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3cac79b521fcca047aff604c7605370b 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Pink.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bdf779d292545fe47ac63467c4191e74 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors/Purple.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 055485c9a0be4ec4d9d4e87820eb713d 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Materials/Colors.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0338e8213bf70c54e93130ccc033b2f3 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AutoMotion 2 | ========== 3 | 4 | A simple yet powerful set of functions for creating smooth motion, tweens, and bouncy effects. Bring your objects to life with just a single line of code! 5 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Scripts/Auto.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f88b3f33399d91748864a5c61874844e 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scripts/AutoMover.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 48729dbaecaf49640a2390a4f6fca038 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scripts/PathMover.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dc560398c59a8fa4c8deca2aa0e5a2b9 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /AutoMotion.userprefs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scripts/PathMover.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class PathMover : MonoBehaviour 5 | { 6 | public EaseType moveEase; 7 | public EaseType stopEase; 8 | public float moveDuration; 9 | public float stopTweenDuration; 10 | public float stopDuration; 11 | public Vector3 stopScale = Vector3.one; 12 | public Vector3[] points; 13 | public int startIndex; 14 | 15 | void Start() 16 | { 17 | StartCoroutine(MoveOnPath()); 18 | } 19 | 20 | IEnumerator MoveOnPath() 21 | { 22 | int index = startIndex; 23 | transform.localPosition = points[index]; 24 | while (true) 25 | { 26 | index = (index + 1) % points.Length; 27 | transform.LookAt(points[index]); 28 | yield return StartCoroutine(transform.MoveTo(points[index], moveDuration, moveEase)); 29 | if (stopTweenDuration > 0) 30 | StartCoroutine(transform.ScaleFrom(stopScale, stopTweenDuration, stopEase)); 31 | if (stopDuration > 0) 32 | yield return StartCoroutine(Auto.Wait(stopDuration)); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 UnityPatterns 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Assets/AutoMotion/Demo/Scripts/AutoMover.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class AutoMover : MonoBehaviour 5 | { 6 | public enum MoveType { Loop, Wave } 7 | 8 | public MoveType moveType; 9 | public float duration; 10 | [Range(0, 1)] public float offset; 11 | public Vector3 fromPosition; 12 | public Vector3 toPosition; 13 | public Vector3 fromRotation; 14 | public Vector3 toRotation; 15 | public Vector3 fromScale = Vector3.one; 16 | public Vector3 toScale = Vector3.one; 17 | 18 | void Start() 19 | { 20 | if (fromPosition != toPosition) 21 | StartCoroutine(Position()); 22 | if (fromRotation != toRotation) 23 | StartCoroutine(Rotation()); 24 | if (fromScale != toScale) 25 | StartCoroutine(Scale()); 26 | } 27 | 28 | IEnumerator Position() 29 | { 30 | var from = transform.localPosition + fromPosition; 31 | var to = transform.localPosition + toPosition; 32 | if (moveType == MoveType.Loop) 33 | { 34 | while (true) 35 | { 36 | transform.localPosition = Auto.Loop(duration, from, to, offset); 37 | yield return 0; 38 | } 39 | } 40 | else 41 | { 42 | while (true) 43 | { 44 | transform.localPosition = Auto.Wave(duration, from, to, offset); 45 | yield return 0; 46 | } 47 | } 48 | } 49 | 50 | IEnumerator Rotation() 51 | { 52 | var from = transform.localRotation * Quaternion.Euler(fromRotation); 53 | var to = transform.localRotation * Quaternion.Euler(toRotation); 54 | if (moveType == MoveType.Loop) 55 | { 56 | while (true) 57 | { 58 | transform.localRotation = Auto.Loop(duration, from, to, offset); 59 | yield return 0; 60 | } 61 | } 62 | else 63 | { 64 | while (true) 65 | { 66 | transform.localRotation = Auto.Wave(duration, from, to, offset); 67 | yield return 0; 68 | } 69 | } 70 | } 71 | 72 | IEnumerator Scale() 73 | { 74 | var from = fromScale; 75 | var to = toScale; 76 | if (moveType == MoveType.Loop) 77 | { 78 | while (true) 79 | { 80 | transform.localScale = Auto.Loop(duration, from, to, offset); 81 | yield return 0; 82 | } 83 | } 84 | else 85 | { 86 | while (true) 87 | { 88 | transform.localScale = Auto.Wave(duration, from, to, offset); 89 | yield return 0; 90 | } 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /Assets/AutoMotion/Scripts/Auto.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public delegate bool Predicate(); 5 | public delegate float Easer(float t); 6 | 7 | public static class Auto 8 | { 9 | #region Transform coroutines 10 | 11 | public static IEnumerator MoveTo(this Transform transform, Vector3 target, float duration, Easer ease) 12 | { 13 | float elapsed = 0; 14 | var start = transform.localPosition; 15 | var range = target - start; 16 | while (elapsed < duration) 17 | { 18 | elapsed = Mathf.MoveTowards(elapsed, duration, Time.deltaTime); 19 | transform.localPosition = start + range * ease(elapsed / duration); 20 | yield return 0; 21 | } 22 | transform.localPosition = target; 23 | } 24 | public static IEnumerator MoveTo(this Transform transform, Vector3 target, float duration) 25 | { 26 | return MoveTo(transform, target, duration, Ease.Linear); 27 | } 28 | public static IEnumerator MoveTo(this Transform transform, Vector3 target, float duration, EaseType ease) 29 | { 30 | return MoveTo(transform, target, duration, Ease.FromType(ease)); 31 | } 32 | 33 | public static IEnumerator MoveFrom(this Transform transform, Vector3 target, float duration, Easer ease) 34 | { 35 | var start = transform.localPosition; 36 | transform.localPosition = target; 37 | return MoveTo(transform, start, duration, ease); 38 | } 39 | public static IEnumerator MoveFrom(this Transform transform, Vector3 target, float duration) 40 | { 41 | return MoveFrom(transform, target, duration, Ease.Linear); 42 | } 43 | public static IEnumerator MoveFrom(this Transform transform, Vector3 target, float duration, EaseType ease) 44 | { 45 | return MoveFrom(transform, target, duration, Ease.FromType(ease)); 46 | } 47 | 48 | public static IEnumerator ScaleTo(this Transform transform, Vector3 target, float duration, Easer ease) 49 | { 50 | float elapsed = 0; 51 | var start = transform.localScale; 52 | var range = target - start; 53 | while (elapsed < duration) 54 | { 55 | elapsed = Mathf.MoveTowards(elapsed, duration, Time.deltaTime); 56 | transform.localScale = start + range * ease(elapsed / duration); 57 | yield return 0; 58 | } 59 | transform.localScale = target; 60 | } 61 | public static IEnumerator ScaleTo(this Transform transform, Vector3 target, float duration) 62 | { 63 | return ScaleTo(transform, target, duration, Ease.Linear); 64 | } 65 | public static IEnumerator ScaleTo(this Transform transform, Vector3 target, float duration, EaseType ease) 66 | { 67 | return ScaleTo(transform, target, duration, Ease.FromType(ease)); 68 | } 69 | 70 | public static IEnumerator ScaleFrom(this Transform transform, Vector3 target, float duration, Easer ease) 71 | { 72 | var start = transform.localScale; 73 | transform.localScale = target; 74 | return ScaleTo(transform, start, duration, ease); 75 | } 76 | public static IEnumerator ScaleFrom(this Transform transform, Vector3 target, float duration) 77 | { 78 | return ScaleFrom(transform, target, duration, Ease.Linear); 79 | } 80 | public static IEnumerator ScaleFrom(this Transform transform, Vector3 target, float duration, EaseType ease) 81 | { 82 | return ScaleFrom(transform, target, duration, Ease.FromType(ease)); 83 | } 84 | 85 | public static IEnumerator RotateTo(this Transform transform, Quaternion target, float duration, Easer ease) 86 | { 87 | float elapsed = 0; 88 | var start = transform.localRotation; 89 | while (elapsed < duration) 90 | { 91 | elapsed = Mathf.MoveTowards(elapsed, duration, Time.deltaTime); 92 | transform.localRotation = Quaternion.Lerp(start, target, ease(elapsed / duration)); 93 | yield return 0; 94 | } 95 | transform.localRotation = target; 96 | } 97 | public static IEnumerator RotateTo(this Transform transform, Quaternion target, float duration) 98 | { 99 | return RotateTo(transform, target, duration, Ease.Linear); 100 | } 101 | public static IEnumerator RotateTo(this Transform transform, Quaternion target, float duration, EaseType ease) 102 | { 103 | return RotateTo(transform, target, duration, Ease.FromType(ease)); 104 | } 105 | 106 | public static IEnumerator RotateFrom(this Transform transform, Quaternion target, float duration, Easer ease) 107 | { 108 | var start = transform.localRotation; 109 | transform.localRotation = target; 110 | return RotateTo(transform, start, duration, ease); 111 | } 112 | public static IEnumerator RotateFrom(this Transform transform, Quaternion target, float duration) 113 | { 114 | return RotateFrom(transform, target, duration, Ease.Linear); 115 | } 116 | public static IEnumerator RotateFrom(this Transform transform, Quaternion target, float duration, EaseType ease) 117 | { 118 | return RotateFrom(transform, target, duration, Ease.FromType(ease)); 119 | } 120 | 121 | public static IEnumerator CurveTo(this Transform transform, Vector3 control, Vector3 target, float duration, Easer ease) 122 | { 123 | float elapsed = 0; 124 | var start = transform.localPosition; 125 | Vector3 position; 126 | float t; 127 | while (elapsed < duration) 128 | { 129 | elapsed = Mathf.MoveTowards(elapsed, duration, Time.deltaTime); 130 | t = ease(elapsed / duration); 131 | position.x = start.x * (1 - t) * (1 - t) + control.x * 2 * (1 - t) * t + target.x * t * t; 132 | position.y = start.y * (1 - t) * (1 - t) + control.y * 2 * (1 - t) * t + target.y * t * t; 133 | position.z = start.z * (1 - t) * (1 - t) + control.z * 2 * (1 - t) * t + target.z * t * t; 134 | transform.localPosition = position; 135 | yield return 0; 136 | } 137 | transform.localPosition = target; 138 | } 139 | public static IEnumerator CurveTo(this Transform transform, Vector3 control, Vector3 target, float duration) 140 | { 141 | return CurveTo(transform, control, target, duration, Ease.Linear); 142 | } 143 | public static IEnumerator CurveTo(this Transform transform, Vector3 control, Vector3 target, float duration, EaseType ease) 144 | { 145 | return CurveTo(transform, control, target, duration, Ease.FromType(ease)); 146 | } 147 | 148 | public static IEnumerator CurveFrom(this Transform transform, Vector3 control, Vector3 start, float duration, Easer ease) 149 | { 150 | var target = transform.localPosition; 151 | transform.localPosition = start; 152 | return CurveTo(transform, control, target, duration, ease); 153 | } 154 | public static IEnumerator CurveFrom(this Transform transform, Vector3 control, Vector3 start, float duration) 155 | { 156 | return CurveFrom(transform, control, start, duration, Ease.Linear); 157 | } 158 | public static IEnumerator CurveFrom(this Transform transform, Vector3 control, Vector3 start, float duration, EaseType ease) 159 | { 160 | return CurveFrom(transform, control, start, duration, Ease.FromType(ease)); 161 | } 162 | 163 | public static IEnumerator Shake(this Transform transform, Vector3 amount, float duration) 164 | { 165 | var start = transform.localPosition; 166 | var shake = Vector3.zero; 167 | while (duration > 0) 168 | { 169 | duration -= Time.deltaTime; 170 | shake.Set(Random.Range(-amount.x, amount.x), Random.Range(-amount.y, amount.y), Random.Range(-amount.z, amount.z)); 171 | transform.localPosition = start + shake; 172 | yield return 0; 173 | } 174 | transform.localPosition = start; 175 | } 176 | public static IEnumerator Shake(this Transform transform, float amount, float duration) 177 | { 178 | return Shake(transform, new Vector3(amount, amount, amount), duration); 179 | } 180 | 181 | #endregion 182 | 183 | #region Waiting coroutines 184 | 185 | public static IEnumerator Wait(float duration) 186 | { 187 | while (duration > 0) 188 | { 189 | duration -= Time.deltaTime; 190 | yield return 0; 191 | } 192 | } 193 | 194 | public static IEnumerator WaitUntil(Predicate predicate) 195 | { 196 | while (!predicate()) 197 | yield return 0; 198 | } 199 | 200 | #endregion 201 | 202 | #region Time-based motion 203 | 204 | public static float Loop(float duration, float from, float to, float offsetPercent) 205 | { 206 | var range = to - from; 207 | var total = (Time.time + duration * offsetPercent) * (Mathf.Abs(range) / duration); 208 | if (range > 0) 209 | return from + Time.time - (range * Mathf.FloorToInt((Time.time / range))); 210 | else 211 | return from - (Time.time - (Mathf.Abs(range) * Mathf.FloorToInt((total / Mathf.Abs(range))))); 212 | } 213 | public static float Loop(float duration, float from, float to) 214 | { 215 | return Loop(duration, from, to, 0); 216 | } 217 | public static Vector3 Loop(float duration, Vector3 from, Vector3 to, float offsetPercent) 218 | { 219 | return Vector3.Lerp(from, to, Loop(duration, 0, 1, offsetPercent)); 220 | } 221 | public static Vector3 Loop(float duration, Vector3 from, Vector3 to) 222 | { 223 | return Vector3.Lerp(from, to, Loop(duration, 0, 1)); 224 | } 225 | public static Quaternion Loop(float duration, Quaternion from, Quaternion to, float offsetPercent) 226 | { 227 | return Quaternion.Lerp(from, to, Loop(duration, 0, 1, offsetPercent)); 228 | } 229 | public static Quaternion Loop(float duration, Quaternion from, Quaternion to) 230 | { 231 | return Quaternion.Lerp(from, to, Loop(duration, 0, 1)); 232 | } 233 | 234 | public static float Wave(float duration, float from, float to, float offsetPercent) 235 | { 236 | var range = (to - from) / 2; 237 | return from + range + Mathf.Sin(((Time.time + duration * offsetPercent) / duration) * (Mathf.PI * 2)) * range; 238 | } 239 | public static float Wave(float duration, float from, float to) 240 | { 241 | return Wave(duration, from, to, 0); 242 | } 243 | public static Vector3 Wave(float duration, Vector3 from, Vector3 to, float offsetPercent) 244 | { 245 | return Vector3.Lerp(from, to, Wave(duration, 0, 1, offsetPercent)); 246 | } 247 | public static Vector3 Wave(float duration, Vector3 from, Vector3 to) 248 | { 249 | return Vector3.Lerp(from, to, Wave(duration, 0, 1)); 250 | } 251 | public static Quaternion Wave(float duration, Quaternion from, Quaternion to, float offsetPercent) 252 | { 253 | return Quaternion.Lerp(from, to, Wave(duration, 0, 1, offsetPercent)); 254 | } 255 | public static Quaternion Wave(float duration, Quaternion from, Quaternion to) 256 | { 257 | return Quaternion.Lerp(from, to, Wave(duration, 0, 1)); 258 | } 259 | 260 | #endregion 261 | } 262 | 263 | #region Easing functions 264 | 265 | public enum EaseType { Linear, QuadIn, QuadOut, QuadInOut, CubeIn, CubeOut, CubeInOut, BackIn, BackOut, BackInOut, ExpoIn, ExpoOut, ExpoInOut, SineIn, SineOut, SineInOut, ElasticIn, ElasticOut, ElasticInOut } 266 | 267 | public static class Ease 268 | { 269 | public static readonly Easer Linear = (t) => { return t; }; 270 | public static readonly Easer QuadIn = (t) => { return t * t; }; 271 | public static readonly Easer QuadOut = (t) => { return 1 - QuadIn(1 - t); }; 272 | public static readonly Easer QuadInOut = (t) => { return (t <= 0.5f) ? QuadIn(t * 2) / 2 : QuadOut(t * 2 - 1) / 2 + 0.5f; }; 273 | public static readonly Easer CubeIn = (t) => { return t * t * t; }; 274 | public static readonly Easer CubeOut = (t) => { return 1 - CubeIn(1 - t); }; 275 | public static readonly Easer CubeInOut = (t) => { return (t <= 0.5f) ? CubeIn(t * 2) / 2 : CubeOut(t * 2 - 1) / 2 + 0.5f; }; 276 | public static readonly Easer BackIn = (t) => { return t * t * (2.70158f * t - 1.70158f); }; 277 | public static readonly Easer BackOut = (t) => { return 1 - BackIn(1 - t); }; 278 | public static readonly Easer BackInOut = (t) => { return (t <= 0.5f) ? BackIn(t * 2) / 2 : BackOut(t * 2 - 1) / 2 + 0.5f; }; 279 | public static readonly Easer ExpoIn = (t) => { return (float)Mathf.Pow(2, 10 * (t - 1)); }; 280 | public static readonly Easer ExpoOut = (t) => { return 1 - ExpoIn(t); }; 281 | public static readonly Easer ExpoInOut = (t) => { return t < .5f ? ExpoIn(t * 2) / 2 : ExpoOut(t * 2) / 2; }; 282 | public static readonly Easer SineIn = (t) => { return -Mathf.Cos(Mathf.PI / 2 * t) + 1; }; 283 | public static readonly Easer SineOut = (t) => { return Mathf.Sin(Mathf.PI / 2 * t); }; 284 | public static readonly Easer SineInOut = (t) => { return -Mathf.Cos(Mathf.PI * t) / 2f + .5f; }; 285 | public static readonly Easer ElasticIn = (t) => { return 1 - ElasticOut(1 - t); }; 286 | public static readonly Easer ElasticOut = (t) => { return Mathf.Pow(2, -10 * t) * Mathf.Sin((t - 0.075f) * (2 * Mathf.PI) / 0.3f) + 1; }; 287 | public static readonly Easer ElasticInOut = (t) => { return (t <= 0.5f) ? ElasticIn(t * 2) / 2 : ElasticOut(t * 2 - 1) / 2 + 0.5f; }; 288 | 289 | public static Easer FromType(EaseType type) 290 | { 291 | switch (type) 292 | { 293 | case EaseType.Linear: return Linear; 294 | case EaseType.QuadIn: return QuadIn; 295 | case EaseType.QuadOut: return QuadOut; 296 | case EaseType.QuadInOut: return QuadInOut; 297 | case EaseType.CubeIn: return CubeIn; 298 | case EaseType.CubeOut: return CubeOut; 299 | case EaseType.CubeInOut: return CubeInOut; 300 | case EaseType.BackIn: return BackIn; 301 | case EaseType.BackOut: return BackOut; 302 | case EaseType.BackInOut: return BackInOut; 303 | case EaseType.ExpoIn: return ExpoIn; 304 | case EaseType.ExpoOut: return ExpoOut; 305 | case EaseType.ExpoInOut: return ExpoInOut; 306 | case EaseType.SineIn: return SineIn; 307 | case EaseType.SineOut: return SineOut; 308 | case EaseType.SineInOut: return SineInOut; 309 | case EaseType.ElasticIn: return ElasticIn; 310 | case EaseType.ElasticOut: return ElasticOut; 311 | case EaseType.ElasticInOut: return ElasticInOut; 312 | } 313 | return Linear; 314 | } 315 | } 316 | 317 | #endregion --------------------------------------------------------------------------------