Hello.
I am trying to get an object to smoothly interpolate and loop between 3 waypoints (which can be moved during gameplay). Am getting close, but there seems to be some significant framerate drop and inconsistent speeds occurring. Here is my code so far. Am a bit of a noob so this might be completely the wrong approach. Has anyone got any suggestions? Thanks.
var waypoint1 : Transform;
var waypoint2 : Transform;
var waypoint3 : Transform;
var i = 0.0;
var i2 = 0.0;
var i3 = 0.0;
var speedbar = 1.0;
var speedbar2 = 1.0;
var speedbar3 = 1.0;
function Start () {
DelayAndDo();
}
function DelayAndDo () {
var dist = Vector3.Distance(waypoint1.position, waypoint2.position);
for (i = 0.0; i < 1.0; i += (speedbar * 0.1 * Time.deltaTime) / dist) {
transform.position = Vector3.Lerp(waypoint1.position, waypoint2.position, i);
yield;
DelayAndDo2 ();
}}
function DelayAndDo2 () {
var dist2 = Vector3.Distance(waypoint2.position, waypoint3.position);
for (i2 = 0.0; i2 < 1.0; i2 += (speedbar2 * 0.1 * Time.deltaTime) / dist2) {
transform.position = Vector3.Lerp(waypoint2.position, waypoint3.position, i2);
yield;
DelayAndDo3 ();
}
}
function DelayAndDo3 () {
var dist3 = Vector3.Distance(waypoint3.position, waypoint1.position);
for (i3 = 0.0; i3 < 1.0; i3 += (speedbar2 * 0.1 * Time.deltaTime) / dist3) {
transform.position = Vector3.Lerp(waypoint3.position, waypoint1.position, i3);
yield;
}
}
function Update (){
if (i3 >= 1.0){
DelayAndDo ();
}}