- Home /
3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp
I am able to get my gameObject to ease in, stay at a constant speed, then ease out based on where the gameObject is along the distance path.
The problem is, I can't get the ease in and ease out lerps to flow smoothing into and out of the steady pace. My code:
Definitions:
originVector = new Vector3(currentTile.transform.position.x, 0, currentTile.transform.position.z); destVector = new Vector3(targetTile.transform.position.x, 0, targetTile.transform.position.z); easeInVector = ((destVector - originVector).normalized 0.5f) + originVector; easeOutVector = ((originVector - destVector).normalized 0.5f) + destVector; totalDistance = Vector3.Distance(originVector, destVector);
IEnumerator UnitEaseIn() { float currentDistance = 0.01f; while (currentDistance <= 0.5f) { unit.position = Vector3.Lerp(unit.position, easeInVector, (currentDistance * 0.35f) / 0.5f); currentDistance += Vector3.Distance(unit.position, originVector); Debug.Log(currentDistance); yield return null; } unit.position = easeInVector; StopCoroutine(unitMvmtCoroutine); unitMvmtCoroutine = StartCoroutine(UnitSteady()); } IEnumerator UnitSteady() { float currentDistance = 0.01f; while (currentDistance <= steadyDistance - 0.5f) { unit.position = Vector3.MoveTowards(unit.position, easeOutVector, Time.deltaTime); currentDistance = Vector3.Distance(unit.position, easeInVector); Debug.Log(currentDistance); yield return null; } unit.position = easeOutVector; StopCoroutine(unitMvmtCoroutine); unitMvmtCoroutine = StartCoroutine(UnitEaseOut()); } IEnumerator UnitEaseOut() { float currentDistance = 0.01f; while (currentDistance <= 0.5f) { unit.position = Vector3.Lerp(unit.position, destVector, EaseOut((currentDistance * 0.35f) / 0.5f)); currentDistance += Vector3.Distance(unit.position, easeOutVector); Debug.Log(currentDistance); yield return null; } unit.position = destVector; StopCoroutine(unitMvmtCoroutine); }
Your answer
Follow this Question
Related Questions
tell me whats wrong with this lerp 1 Answer
Smooth swapping my game object 0 Answers
Weapon Movement Freezing 0 Answers
Movement Smoothing Script not Working 2 Answers
Adding curve to Vector3.MoveTowards 1 Answer