Skip to content

Instantly share code, notes, and snippets.

@Naxum
Created May 7, 2014 16:15
Show Gist options
  • Save Naxum/74933b22851ba130cdda to your computer and use it in GitHub Desktop.
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!
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;
}
}
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