# Lerping back and forth between two vectors?

Hi all,

I’m trying to make a checkpoint object in my game lerp back and forth between two vectors, so that it appears to be floating up and down slowly in the gameworld. I attempted to use “Vector3.MoveTowards()”, but that made it slide back and forth too fast and it looked very wrong.

This is what I’ve come up with, however there is one issue:

``````using UnityEngine;
using System.Collections;

public class CheckPointFloating : MonoBehaviour
{
public float speed;
public Vector3 startPosition;
public Vector3 endPosition;
private Vector3 target;

void Start()
{
target = endPosition;
}
// Update is called once per frame
void Update ()
{

// Move between the start and end vectors
if(transform.position == endPosition)
{
target = startPosition;
}else if (transform.position == startPosition)
{
target = endPosition;
}

// Update position
transform.position = Vector3.Lerp(transform.position, target, speed * Time.deltaTime);

}
}
``````

The problem I’m having is that I’m checking to see if the current position of the object matches either the start or end vector, and when using the lerp function this produces the undesired result of the checkpoint object staying in one spot for about 5 seconds before it does equal either the start or end vector, and reverses the direction. I can’t think of a way to check if the checkpoint object is at either vector that would allow me to use the Vector3.Lerp() function. Is there a way to say “if the checkpoint is at roughly this destination, reverse direction” in code that I’m just not seeing?

Any help is appreciated! Thanks!

Yup there sure is. You could use mathf.approximately: Unity - Scripting API: Mathf.Approximately

I don’t really like using it though, it doesn’t always return true so I use my own version which has a tolerance value:

``````    bool isApproximate (float inputA, float inputB, float tollerance) {
return Mathf.Abs (inputA - inputB) < tollerance;
}
``````

Orrrr alternatively you could even use Vector3.Distance to check if your object is within a certain distance of your vector

Wow, thank you smitchell! That works quite well, and I definitely like the addition of the tolerance, makes things a little easier to customize!

This is what my final script looks like:

``````using UnityEngine;
using System.Collections;

public class CheckPointFloating : MonoBehaviour
{
public float speed;
public float tolerance;
public float startYPos;
public float endYPos;
private float target;
private Vector3 position;

// Update is called once per frame
void Update ()
{
// Get current position
position = transform.position;
float curYPos = position.y;

// Move between the start and end vectors
if(isApproximate(curYPos, endYPos, tolerance))
{
target = startYPos;
}else if (isApproximate(curYPos, startYPos, tolerance))
{
target = endYPos;
}

// Update position
transform.position = Vector3.Lerp(position, new Vector3(position.x, target, position.z), speed * Time.deltaTime);

}

bool isApproximate(float a, float b, float tolerance)
{
return Mathf.Abs (a - b) < tolerance;
}
}
``````

That can still fail if the framerate happens to hiccup at the wrong time, so I would suggest never relying on comparing values like that. Just lerp back and forth between the two positions; coroutines are ideal.

``````IEnumerator BackNForth () {
while (true) {
// lerp between point A and point B in a loop
// then lerp between point B and point A in a loop
}
}
``````

–Eric

I actually just thought of a great solution!

Using something I borrowed from the physics tutorials, I made it so that the checkpoint stays within a trigger zone, where a constant (and limited) force pushes up on it, creating EXACTLY the effect I was going for!

Here’s the code…

``````using UnityEngine;
using System.Collections;

public class HoveringObject : MonoBehaviour
{
public float hoverForce;
public float maxForce;

void OnTriggerStay(Collider other)
{
if(other.tag == "CheckPoint")
{