TraversePath.cs 745 Bytes
Newer Older
Zac Olsen's avatar
Zac Olsen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
using UnityEngine;
using System.Collections;

public class TraversePath : MonoBehaviour {

	public float speed;
	public float pathErrorRange;

	private float speedLerpDistance;

	private Path path;

	void Start () {

		speedLerpDistance = speed * Time.fixedDeltaTime;
	}
	
	void FixedUpdate () {
	
		if (path != null && !path.IsEmpty ()) {

			Vector3 tilePos = path.Peek ().transform.position;
			float distance = Vector3.Distance (transform.position, tilePos);

			transform.position = Vector3.Lerp (transform.position, tilePos, 1 / distance * speedLerpDistance);

			if (distance <= pathErrorRange) {
				path.Pop ();
			}

		}
	}

	//path must begin on current tile
	public void BeginTraversingPath (Path path) {
		this.path = path;
	}
}