-
-
Save AlexMeesters/1cc06cf521585dbe5babb87824c1c814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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