# How do I LERP from one position to another relative to a target distance?

Discussion in 'Getting Started' started by JapaneseBreakfast, Sep 8, 2018.

1. ### JapaneseBreakfast

Joined:
Sep 7, 2018
Posts:
41
Hey there community, I'm new to the forums and Unity, pleased to meet you all I'm trying to do a simple control mechanic, but can't seem to figure it out:

The Problem

My main character (blue cube) automatically moves forward in the world like on rails none-stop, and the player can only translate left, right or stay in the middle between three positions that I'm representing as game objects (red cubes).

Player does this by tapping on one of the red boxes next to the character. Kind of like switching train tracks. So the red boxes are just used as transform.position targets. The player doesn't move forward quicker, he just lerps left or right quickly or slowly depending on how far he is from the next target cubes. What I'm trying to accomplish

What I'm trying to do is LERP the player's blue cube left or right between one of the red target cubes, but the velocity it takes to reach the target (left or right) should be relative to the distance to the next red cubes. So the character needs to reach the target (left or right) before it reaches the next red cubes.

In other words, if the player is close to the next red cubes, the character should lerp quicker left or right. But if the character is at a further distance, the character would lerp slower since it has more time to reach the target.

I really hope this made sense. If I didn't explain myself properly, please excuse me, I'm learning. Thanks a lot in advance!

2. ### lipisis

Joined:
Jan 14, 2017
Posts:
20
Here is the idea I came up with. You should be able to implement it in your script really easily. I believe I explained all of this in too much detail so it looks really long, but it's really simple at its' core so no worries.

First of all let's have a look at most basic mathematical case: Red line represents 3 red target cubes that are coming to our blue cube. Blue square is our starting position. Blue dotted square our desired final position.

In most basic mathematical scenario we have three constants.
• d - distance between our current position and target cubes
• x - distance between our current position and our desired final position
• speed - some sort of velocity (speed) that red line is coming to us.
When user taps on one of the two red cubes in your example (left, right) we only need to calculate the amount of units (dx) we need to move our current position towards the desired position in each frame. Where dx = x/distance * speed.

After calculating dx all we need to do is move our main cube towards desired position by dx amount of units each frame.

(Two examples are given to make this even more clear. If distance to our desired position is 5 units , targets are 10 units away, and we come closer to those targets 1 unit/frame it means we need to move 0.5 unit each frame to make it just in time)

This would work only if all our variables are constants and they do not change in any frame. But in our case it's most likely that our speed variable depends on time between each frame (aka
``Time.deltaTime``
). In such case we need another solution which is quite similar to the first one.

Let's see second example: In this case we need to introduce new variable:
• speed% - the percentage expression of the distance that targets moved closer to our current position.
If distance (d) is 10 and red line moved 1 (speed) unit closer it means we moved 10% (speed%) of our distance.

Now we need to move to our desired position same amount of percentage (speed%) we targets came closer to us.

So the final formula of dx would be:
``dx=speed% * x``
where
``speed%=speed/distance * 100``

On the right side of the picture you can see the simulation of a frame drop (speed goes up since Time.deltaTime is getting bigger).

To be 100% correct, you should check if speed% is more (or >= actually) than 100% so it means red line reached us so we need to be in our desired position.

Good luck, and welcome to Unity forum.
Cheers!

Last edited: Sep 8, 2018
JapaneseBreakfast likes this.
3. ### lipisis

Joined:
Jan 14, 2017
Posts:
20
Forgot to mention that in second situation we need to recalculations each frame

JapaneseBreakfast likes this.
4. ### JoeStrout

Joined:
Jan 14, 2011
Posts:
8,138
It sounds to me like you simply want to move at a constant speed, so it takes longer to go a longer distance. You do that trivially with Vector3.MoveTowards.

JapaneseBreakfast likes this.

Joined:
Sep 7, 2018
Posts:
41