Created
May 7, 2014 16:15
-
-
Save Naxum/74933b22851ba130cdda to your computer and use it in GitHub Desktop.
Simple Coroutine! One simple coroutine, another utilizing an animation curve to smooth the lerp in a sexy way!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Unit : MonoBehaviour | |
{ | |
public Transform target; | |
void Start(){ | |
StartCoroutine(MoveTo(target)) | |
} | |
IEnumerator MoveTo(Transform target) | |
{ | |
float t = 0; | |
float moveTime = 0.35f; | |
Vector3 start = this.transform.position; | |
Vector3 end = target.transform.position; | |
while (t < 1) | |
{ | |
this.transform.position = Vector3.Lerp(start, end, t); | |
t += Time.deltaTime / moveTime; | |
yield return null; | |
} | |
this.transform.position = end; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Unit : MonoBehaviour | |
{ | |
public Transform target; | |
public AnimationCurve smoothCurve; | |
public float moveTime = 0.35f; | |
void Start(){ | |
StartCoroutine(MoveTo(target)) | |
} | |
IEnumerator MoveTo(Transform target) | |
{ | |
float t = 0; | |
Vector3 start = this.transform.position; | |
Vector3 end = target.transform.position; | |
while (t < 1) | |
{ | |
this.transform.position = Vector3.Lerp(start, end, smoothCurve.Evaluate(t)); | |
t += Time.deltaTime / moveTime; | |
yield return null; | |
} | |
this.transform.position = end; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment