My apps

Mac App Store

Tuckerton Pool
Unity3D C#
NodeJS
SocketIO

iOS App Store

Philly Bike Dock
Swift

Rhythasym
Objective-C

Tuckerton Pool
Unity3D C#
NodeJS
SocketIO

Jack's Gauntlet
Unity3D

Amoratis 7
Unity3D

Android
Play Store

Jack's Gauntlet
Unity3D

I created the custom Bootstrap WordPress theme this site is using.

#### CHMOD Converter

I made this CHMOD WordPress widget.
Get it at wordpress.org

# Unity C#: You Can LERP if You Want To

Imagine that you want to move an elevator platform from X1,Y1 to X2,Y2 on the screen in 1 second.

You might reason:

30 fps (frames per second)

1.0 seconds / 30 frames = .033 seconds per frame

100% of movement = 100% of 1 second

There is a 1:1 correlation.

therefore

.033 of movement = .033 of 1 second

Result: The box moves .033 of the distance between the cubes each frame. Cumulatively, all those .033 incremental moves will add up to 100% of the distance between X1, Y1 and X2,Y2.

This is a fine approach for something like a video where you can be sure that the frames will be played on time, every time. But for a live phone app where everything is being rendered and moving on the fly, the frame rate can change. If the iPhone has another app running a background task or if your app is doing a lot of things, then the frame rate could drop far below 30 fps. You might be okay with “approximately” two seconds for the square to move, but what if we change devices all together? A different model or generation of iPhone might only run 20 fps.

Because of these things, it is often best not to associate rate of movement with frame rate.

We need to associate the change in movement with the change in time. So when each new frame is shown, we need the delta of the time since the last frame was shown. The delta is the change in time from one frame to the next. We can use this time delta to set a pace for the linear interpolation of an object.

Linear Interpolation

(The following example assumes two ‘place marker’ gameobjects are already placed into the scene along with the platform to be moved. It is also envisioned that all of these are associated with the GameObject variables here in this sample script.)

using UnityEngine;
using System.Collections;

public class moveSphere : MonoBehaviour {

public GameObject startPlaceholderObject;
public GameObject stopPlaceholderObject;
private float startTime;

// Use this for initialization
void Start () {
startTime = Time.time;
gameObject.transform.position = startPlaceholderObject.transform.position;
}

// Update is called once per frame
void Update () {
float timeDiff = (Time.time - startTime);
timeDiff = timeDiff / 10.0f;
gameObject.transform.position = Vector3.Lerp(startPlaceholderObject.transform.position,stopPlaceholderObject.transform.position,timeDiff);
}
}


That’s it!  The Update() method runs every time a frame is shown. So each time a frame is shown, your program determines how long it has been since the last frame was shown. A calculation is made to determine how much of the final time that increment equals. Then it moves your object along the Vector3 path by that percentage.

Delta, Delta, give me the news!  I got a bad case of loving you.

Change of any changeable quantity, in mathematics and the sciences (more specifically, the difference operator[citation needed]); for example, in:
{y_2-y_1\over x_2-x_1} = {\Delta y \over \Delta x} ,
the average change of y per unit x (i.e. the change of y over the change of x). Delta is the initial letter of the Greek word διαφορά diaphorá, “difference”. (The small Latin letter d is used in much the same way for the notation of derivatives and differentials, which also describe change.)