Skip to content

Instantly share code, notes, and snippets.

@arkms
Created November 8, 2016 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkms/45a4b3fa5f3fbc1ff05370314de699b7 to your computer and use it in GitHub Desktop.
Save arkms/45a4b3fa5f3fbc1ff05370314de699b7 to your computer and use it in GitHub Desktop.
Rieles con Lerp
public float Velocidad;
public Transform[] Puntos;
//Internos
int IndexActual = 0; //Index para mover en puntos
Vector3 PuntoA; //Punto A para Lerp
Vector3 PuntoB; //Punto B para Lerp
float t; //Factor tiempo de Lerp
float factorT; //Factor de moviemnto
void Start()
{
t = 1f; //Esto ayuda al primer calculo
CalcularValores();
}
void Update()
{
t += factorT * Time.deltaTime;
if (t >= 1f) //ya llegamos?
{
IndexActual++;
if(IndexActual == Puntos.Length-1) //Ya es el ultimo tramo?
{
IndexActual = 0;
}
CalcularValores();
}
transform.position = Vector3.Lerp(PuntoA, PuntoB, t);
}
void CalcularValores()
{
PuntoA = Puntos[IndexActual].position;
PuntoB = Puntos[IndexActual + 1].position;
t = 1.0f - t; //Solo para continuar con el movimiento y no se vea brusco una pausa
//FactorT siempre sera diferente entre puntos, pero al final esto ayudara que el moviento siempre sea la misma.
factorT = 1.0f / Vector3.Distance(PuntoA, PuntoB) * Velocidad;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment