- Home /
How do I add multiple points using Lerp?
I want to make multiple waypoints which a gameobject follows and make them random later on. I am currently using the standard lerp snippet but I am absolutely lost as how to add multiple points to the function.
using UnityEngine;
using System.Collections;
public class Waypoints : MonoBehaviour
{
public Transform startMarker;
public Transform endMarker;
public float speed = 1.0F;
private float startTime;
private float journeyLength;
public Transform target;
public float smooth = 5.0F;
void Start ()
{
startTime = Time.time;
journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
}
void Update ()
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
}
}
Getting this to work has obviously been no problem but I could really use some help adding multiple waypoints between the start- and endMarker.
Answer by Baste · Dec 10, 2014 at 03:22 PM
The idea here would be to not have waypoints in between the start and endpoint, as much as having the start and end point being the current last and next waypoint, and switching those when you get to a waypoint
First, you grab an array of the different waypoints you need, and let the start and end marker be private variables:
private Transform startMarker, endMarker;
public Transform[] waypoints;
Remember to order this list by the order of waypoints you want. You can use Gizmos to draw lines between consequtive waypoints, that would make it easier to see them.
Anyways, in Start you set the index of which position is your current start point, and set the current start and end markers. I'm setting the markers in an own method, since it's going to be a thing you do a bunch.
int currentStartPoint;
void Start() {
currentStartPoint = 0;
SetPoints();
}
void SetPoints() {
startMarker = waypoints[currentStartPoint];
endMarker = waypoints[currentStartPoint + 1];
startTime = Time.time;
journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
}
And in update, switch which waypoints are the first and next when you've reached the endPoint - which is when the lerp value is over 1:
void Update() {
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
if(fracJourney >= 1f && currentStartPoint + 1 < waypoints.Length) {
currentStartPoint++;
SetPoints();
}
}
This will move the character from waypoint to waypoint until the last one is reached, at which point the character will stop moving entirely. Hope that helps!
Thank you very much for your fast reply! I am currently trying it out and wanted to tinker with it, but I keep getting the error:
Type
UnityEngine.Transform[]' does > not contain a definition for
Lenght' and no extension methodLenght' of > type
UnityEngine.Transform[]' could be found (are you missing a using directive or an assembly reference?)
I tried a few things but I keep getting some variation or another of the error. Any idea what could solve it?
EDIT: Haha, after quite some googling I found the error! Length was misspelled, I can continue now. Can't believe I didn't notice it!
can i get the same functionality without update() function ?
Answer by Naphier · May 04, 2016 at 04:53 PM
While the answers here work for the most part, these don't really provide lerp functionality. The power of Linear Interpolation (lerp) is that you can feed in a ratio (0 to 1) of the completeness from start point to end point.
Baste's answer will get you from the start point to the end point, but you can't get back. It also doesn't consider that the waypoints could not be in a straight line (his journeyLength is the end - start... well, what if they're the same point?).
Here's a more robust solution that can handle whatever waypoint positions you feed it AND you can feed in any ratio, making it an actual lerp function. Unfortunately, this comes at a cost, but unless your waypoint list has a lot of waypoints you should be OK. I'll work out an optimized one when I have a chance. Check https://github.com/Naphier/NGTools or https://gist.github.com/Naphier/46e066746ee9eeb70ebfd5062dc426bf for updates.
using System;
using UnityEngine;
namespace NG
{
public static class Vector3Ext
{
#region Multilerp
// This is not very optimized. There are at least n + 1 and at most 2n Vector3.Distance
// calls (where n is the number of waypoints).
public static Vector3 MultiLerp(Vector3[] waypoints, float ratio)
{
Vector3 position = Vector3.zero;
float totalDistance = waypoints.MultiDistance();
float distanceTravelled = totalDistance * ratio;
int indexLow = GetVectorIndexFromDistanceTravelled(waypoints, distanceTravelled);
int indexHigh = indexLow + 1;
// we're done
if (indexHigh > waypoints.Length - 1)
return waypoints[waypoints.Length - 1];
// calculate the distance along this waypoint to the next
Vector3[] completedWaypoints = new Vector3[indexLow + 1];
for (int i = 0; i < indexLow + 1; i++)
{
completedWaypoints[i] = waypoints[i];
}
float distanceCoveredByPreviousWaypoints = completedWaypoints.MultiDistance();
float distanceTravelledThisSegment =
distanceTravelled - distanceCoveredByPreviousWaypoints;
float distanceThisSegment = Vector3.Distance(waypoints[indexLow], waypoints[indexHigh]);
float currentRatio = distanceTravelledThisSegment / distanceThisSegment;
position = Vector3.Lerp(waypoints[indexLow], waypoints[indexHigh], currentRatio);
return position;
}
public static float MultiDistance(this Vector3[] waypoints)
{
float distance = 0f;
for (int i = 0; i < waypoints.Length; i++)
{
if (i + 1 > waypoints.Length - 1)
break;
distance += Vector3.Distance(waypoints[i], waypoints[i + 1]);
}
return distance;
}
public static int GetVectorIndexFromDistanceTravelled(
Vector3[] waypoints, float distanceTravelled)
{
float distance = 0f;
for (int i = 0; i < waypoints.Length; i++)
{
if (i + 1 > waypoints.Length - 1)
return waypoints.Length - 1;
float segmentDistance = Vector3.Distance(waypoints[i], waypoints[i + 1]);
if (segmentDistance + distance > distanceTravelled)
{
return i;
}
distance += segmentDistance;
}
return waypoints.Length - 1;
}
#endregion
}
}
Answer by jenci1990 · Dec 10, 2014 at 04:00 PM
This is a simple way for it:
using UnityEngine;
using System.Collections;
public class Waypoints : MonoBehaviour {
void Start() {
Vector3[] positions = {new Vector3(1f,1f,0f), new Vector3(0f,0f,0f), new Vector3(0f,1f,1f)};
StartCoroutine( MultipleLerp(positions, 1f) );
}
IEnumerator MultipleLerp(Vector3[] _pos, float _speed) {
for (int i = 0; i < _pos.Length; i++) {
Vector3 startPos = transform.position;
float timer = 0f;
while (timer <= 1f) {
timer += Time.deltaTime * _speed;
Vector3 newPos = Vector3.Lerp(startPos, _pos[i], timer);
transform.position = newPos;
yield return new WaitForEndOfFrame();
}
transform.position = _pos[i];
startPos = _pos[i];
}
yield return false;
}
}