Created
August 20, 2015 23:30
-
-
Save DanBrooker/18c428e845466f34fd01 to your computer and use it in GitHub Desktop.
Unity3D move to waypoints
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; | |
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