Lerp GameObject Forward And Backward Of A 3D Object Using C# Scripts In Unity

Introduction

 
In this article, we will discuss Lerp GameObject forward & Backward of a 3D object using C# scripts in Unity.
 
Prerequisite
 
Unity environment version 2019.3.0f6
 
Create the Project
 
 
In the Unity menu, go to GameObject > 3D Object > Cube. A cube is added to the SampleScene in the Hierarchy tab
 
 
Rescale the cube size in Scene View
 
 
Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "cube" option. The cube will be added to the scene View
 
 
Select the cube and set the position in Scene View
 
 
Create an Empty Gameobject
 
Select the Game Object >> Create an empty GameObject.
 
 
Select the empty GameObject on click (Ctrl + D). Create a duplicate GameObject
 
 
Rename all the objects used for the scene as: Floor, Player, StartPoint, and EndPoint.
 
 
Set the StartPosition in Scene View.
 
 
Set the EndPosition in Scene View.
 
 
Create a Folder
 
Right-click on Assets. Select Create >> Folder.
 
 
Rename the Folder as Materials
 
 
Create a Material
 
Right-click on Assets. Select Create >> Material.
 
 
Rename the material as Floor and Player.
 
 
Drag and drop the floor and player material. The color of the cube will be changed into the colors of the floor and player.
 
 
Create a C# Script
 
Right-click on Assets. Select Create >> C# script.
 
 
Rename the Scripts as Lerper
 
 
Double click on the Lerper script. The MonoDevelop-Unity editor will open up.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class Lerper: MonoBehaviour {  
  5.     public Transform startPos, endPos;  
  6.     public bool repeatable = false;  
  7.     public float speed = 1.0 f;  
  8.     public float duration = 3.0 f;  
  9.     float startTime, totalDistance;  
  10.     // Use this for initialization    
  11.     IEnumerator Start() {  
  12.         startTime = Time.time;  
  13.         totalDistance = Vector3.Distance(startPos.position, endPos.position);  
  14.         while (repeatable) {  
  15.             yield  
  16.             return RepeatLerp(startPos.position, endPos.position, duration);  
  17.             yield  
  18.             return RepeatLerp(endPos.position, startPos.position, duration);  
  19.         }  
  20.     }  
  21.     // Update is called once per frame    
  22.     void Update() {  
  23.         if (!repeatable) {  
  24.             float currentDuration = (Time.time - startTime) * speed;  
  25.             float journeyFraction = currentDuration / totalDistance;  
  26.             this.transform.position = Vector3.Lerp(startPos.position, endPos.position, journeyFraction);  
  27.         }  
  28.     }  
  29.     public IEnumerator RepeatLerp(Vector3 a, Vector3 b, float time) {  
  30.         float i = 0.0 f;  
  31.         float rate = (1.0 f / time) * speed;  
  32.         while (i < 1.0 f) {  
  33.             i += Time.deltaTime * rate;  
  34.             this.transform.position = Vector3.Lerp(a, b, i);  
  35.             yield  
  36.             return null;  
  37.         }  
  38.     }  
  39. }  
Save the program.
 
Drag and drop the Lerper script onto the Player.
 
 
Call the Start & End Position onto Lerper Scripts in Scene View.
 
 
Set the Speed and Duration to Lerper scripts in Scene View.
 
 
Click on the Play button to see Lerper Position of a 3D object in run time:
 
 

Summary

 
I hope you understood how to perform Lerp GameObject forward & Backward of a 3D object using C# scripts in Unity.


Similar Articles