Skip to content

Instantly share code, notes, and snippets.

@DanBrooker
Created August 20, 2015 23:30
Show Gist options
  • Save DanBrooker/18c428e845466f34fd01 to your computer and use it in GitHub Desktop.
Save DanBrooker/18c428e845466f34fd01 to your computer and use it in GitHub Desktop.
Unity3D move to waypoints
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Character : MonoBehaviour {
public int health = 2;
public int actions = 2;
public string name = "";
public float moveSpeed = 0.8f;
float startTime = 0f;
Vector3 startPoint;
Vector3 endPoint;
List<Location> path;
Location waypoint;
// Use this for initialization
void Start () {
path = new List<Location>();
}
// Update is called once per frame
void Update () {
if (waypoint != null) {
moveAlongPath();
}
}
void moveAlongPath() {
var i = (Time.time - startTime) / moveSpeed;
transform.position = Vector3.Lerp(startPoint, endPoint, i);
if(i >= 1) {
nextWaypoint();
}
}
void nextWaypoint() {
if (path.Count == 0) {
waypoint = null;
return;
}
if (waypoint == null) {
endPoint = transform.position;
}
waypoint = path [0];
path.RemoveAt (0);
startTime = Time.time;
startPoint = endPoint;
endPoint = waypoint.vector3 ();
}
public void FollowPath(List<Location> locations) {
this.path = new List<Location>(locations);
nextWaypoint ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment