You might be able to put something together using iTween or lean tween, but I if you needs something really accurate you’d be better using spline package rather than a tween package with a bit of spline on the side. Super Splines in the Asset store has come up favorable recommendations on this list a number of times.
Here is a bit of code that demonstrates how it might be done in iTween. iTween does not provide control points, so I use 5 points rather than 3. To use start with an new scene and do:
- Create an empty game object, add a LineRenderer component, set the beginning and ending line width to 0.1, add a material if you like:
- Create a sphere, size the sphere to 0.4,0.4 0.4.
- Add the script below to the sphere, and drag and drop the game object with the line renderer on the ‘Lr’ variable.
- Hit play
#pragma strict
var threshold : float = 0.1;
var radius = 5.5;
var lr : LineRenderer;
var speed : float = 2.0;
private var markers : Transform[] = new Transform[4];
private var path : Vector3[] = new Vector3[4];
private var fraction : float = 0.0;
private var _speed : float;
private var boundary : float = 2.0 / 3.0;
function Start() {
for (var i = 0; i < path.Length; i++) {
path _= Random.insideUnitSphere * radius;_
-
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);*
-
go.transform.localScale = Vector3(0.2, 0.2, 0.2);*
_ go.transform.position = path*;_
_ markers = go.transform;
}*_
* SetLineRenderer();*
* var l = GetPathLength();*
* speed = speed / GetPathLength();*
}_
function Update() {
* transform.position = iTween.PointOnPath(path, fraction);*
fraction += Time.deltaTime * _speed;
* if (fraction > boundary) {*
* path[0] = path[1];*
* path[1] = transform.position;*
* path[2] = path[3];*
_ path[3] = Random.insideUnitSphere * radius;_
* fraction = 1.0/3.0;*
* _speed = speed / GetPathLength();*
* SetLineRenderer();*
* SetMarkers();*
* }*
}
function SetMarkers() {
* for (var i = 0; i < markers.Length; i++)*
markers_.position = path*;
}*_
function SetLineRenderer() {
* lr.SetVertexCount(101);*
* for (var i = 0; i < 101; i++) {*
* var f : float = i / 100.0;*
* lr.SetPosition(i, iTween.PointOnPath(path, f));*
* }*
}
function GetPathLength() : float {
* var length = 0.0;*
* for (var i = 1; i < path.Length; i++) {*
_ length += Vector3.Distance(path[i-1], path*);
}
return length;
}*
A good deal of this code deals with putting marking cubes and lines to see the path and nodes and has nothing to do with walking the path. Note I determined empirically that there is a direct map between fraction and vertex position. That is, given five point in the path, point 3 will always be at fraction 0.5, and point 4 will always be at 0.75. This makes this code far easier (given the limited interface in iTween for paths) to write, but it also opens up a problem. That is, given random points of varying distance in the path, the object will travel slower or faster depending on the distance between the control points. I calculate a speed for the whole path, but the right thing to do (if you care), is to set the speed based on the distance in the current path segment.
Oh, and you need to have downloaded iTween (its free)._