Home / Lua / Shared / math / lerp


Returns    number
Prototype    math.lerp(number start, number end, number fraction)
Description    Performs a linear interpolation between start and end using fraction.


Returns    Angle
Prototype    math.lerp(Angle start, Angle end, Angle fraction)
Description    Performs a linear interpolation between start and end using fraction.


Returns    Color
Prototype    math.lerp(Color start, Color end, Color fraction)
Description    Performs a linear interpolation between start and end using fraction.


Returns    Vector2
Prototype    math.lerp(Vector2 start, Vector2 end, Vector2 fraction)
Description    Performs a linear interpolation between start and end using fraction.


Returns    Vector3
Prototype    math.lerp(Vector3 start, Vector3 end, Vector3 fraction)
Description    Performs a linear interpolation between start and end using fraction.

Examples

Bouncy ball example

Creates a red circle that bounces back and forth on your screen
local timer = Timer()
Events:Subscribe("Render", function()
	local frac = math.sin(timer:GetSeconds()) * 0.5 + 0.5 -- X will pass between 0 and 1

	local x = math.lerp(0, Render.Width, frac) -- Interpolate X between 0 and our screen width

	Render:FillCircle(Vector2(x, 50), 5, Color(255,0,0))
end)

Vector3 example

Will draw a circle between you and the last vehicle you sat in.
local lastvehicle
Events:Subscribe("LocalPlayerExitVehicle", function(args)
	local veh = args.vehicle
	
	lastvehicle = veh
end)

local transform
local angle = Angle(0,math.pi/2,0) -- Rotation for the transform for it to look up
local function DrawGroundCircle(pos, radius, color)
 transform = Transform3()
 transform:Translate(pos):Rotate(angle)
 
 Render:SetTransform(transform)
 Render:FillCircle(Vector3(0,0.05,0), radius, Color(0,0,0))
 Render:FillCircle(Vector3(0,0.05,0), radius*0.9, color)
 Render:ResetTransform()
end

Events:Subscribe("Render", function()
	if IsValid(lastvehicle) then
		local plypos = LocalPlayer:GetPosition()
		local vehpos = lastvehicle:GetPosition()
		local middlepos = math.lerp(plypos, vehpos, 0.5) -- Interpolate the vectors and grab the middle.
		DrawGroundCircle(middlepos, 0.5, Color(255,0,0))
	end
end)