Lerp o Interpolación lineal
Lerp o Interpolación lineal, es la linea continua que se genera entre un punto inicial y un punto final. Y se puede sacar cualquier punto de esta recta linea, solo necesitamos un porcentaje donde 0% es el punto inicial y 100% es el punto final. En otras palabras podemos obtener cualquier coordenadas que se encuentre entre estos 2 puntos.
¿Como usar correctamente Lerp?
‘rate’ solo tenemos que sustituir por ‘rateTiempo’ o ‘rateVelocity’ según si necesitamos que recorra eso siempre en un tiempo exacto o se mueva a una velocidad. Y ‘startPos’ son las coordenadas del punto inicial y ‘endPos’ son las coordenadas donde deseamos terminar.
De esta manera, siempre la distancia es la misma y la velocidad constante. Ahora existen 2 rate en este caso, la ‘rateTiempo’ cuando deseas que haga el recorrido en un cierto intervalo de tiempo o ‘rateVelocity’ para hacer el recorrido a una velocidad, cualquiera de las 2 se sustituye por ‘rate’ dentro del if.
La misma teoría se aplica para la rotación, Vectores 2, 3, 4 o incluso solo un lerp entre 2 números.
Leave feedback
public static Vector3 Lerp(Vector3 a, Vector3 b, float t);
a
Start value, returned when t = 0.
b
End value, returned when t = 1.
t
Value used to interpolate between a and b.
Vector3 Interpolated value, equals to a + (b - a) * t.
Linearly interpolates between two points.
Interpolates between the points a and b by the interpolant t. The parameter t is clamped to the range [0, 1]. This is most commonly used to find a point some fraction of the way along a line between two endpoints (e.g. to move an object gradually between those points).
The value returned equals a + (b - a) * t (which can also be written a * (1-t) + b*t).
When t = 0, Vector3.Lerp(a, b, t) returns a.
When t = 1, Vector3.Lerp(a, b, t) returns b.
When t = 0.5, Vector3.Lerp(a, b, t) returns the point midway between a and b.
Hasta aquí! ya sabemos lo necesario en teoría para poder empezar
Vamos a la practica!!
para poder mover un objeto de un punto A a un punto B
en mi caso creare una sphere
el GameObject del punto A indicara la posición inicial o StartPosition por este motivo debe estar en la misma posición q el objeto Sphere
el GameObject del punto B indicara la posición final o enfPosition
luego creamos 2
la pocicion de el GameObject vacio A es la misma de la sphere
la pocicion de el GameObject vacio B
ahora a codificar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lerper : MonoBehaviour
{
public Transform startPos, endPos;
public bool repeatable = false;
public float speed = 1.0f;
float starTime, totalDistance;
IEnumerator Start(){
starTime =Time.time;
totalDistance = Vector3.Distance(startPos.position, endPos.position);
while(repeatable){
yield return RepeatLerp(startPos.position, endPos.position, 3.0f);
yield return RepeatLerp(endPos.position, startPos.position, 3.0f);
}
}
// Update is called once per frame
void Update()
{
if(!repeatable){
float currentDuration = (Time.time - starTime) * speed;
float journeyFraction = (currentDuration / totalDistance);
this.transform.position = Vector3.Lerp(startPos.position, endPos.position ,journeyFraction);
}
}
public IEnumerator RepeatLerp(Vector3 a, Vector3 b, float time){
float i = 0.0f;
float rate = (1.0f / time) * speed;
while (i < 1.0f){
i += Time.deltaTime * rate;
this.transform.position = Vector3.Lerp(a,b,i);
yield return null;
}
}
}
a continuación cargamos el código al la Sphere
y asignamos los GameObjects vacíos
Aquí un pequeño video donde te muestro como debería de funcionar