Skip to content

Instantly share code, notes, and snippets.

@AlexMeesters
Created June 30, 2019 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexMeesters/1cc06cf521585dbe5babb87824c1c814 to your computer and use it in GitHub Desktop.
Save AlexMeesters/1cc06cf521585dbe5babb87824c1c814 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class LerpExample : MonoBehaviour
{
[SerializeField] private Vector3 pointA = new Vector3(-2, 0, 0);
[SerializeField] private Vector3 pointB = new Vector3(2, 0, 0);
[SerializeField] private float speed = 1;
private float t;
private void Update()
{
t += Time.deltaTime * speed;
// Moves the object to target position
transform.position = Vector3.Lerp(pointA, pointB, t);
// Flip the points once it has reached the target
if (t >= 1)
{
var b = pointB;
var a = pointA;
pointA = b;
pointB = a;
t = 0;
}
}
// What Linear interpolation actually looks like in terms of code
private Vector3 CustomLerp(Vector3 a, Vector3 b, float t)
{
return a * (1 - t) + b * t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment