:Lerp doesnt work?

The title explains itself, my code:

rp.CFrame = rp.CFrame:Lerp(CFrame.new(hitPosition), 0) -- rp is humanoid root part

Lerp’s 2nd parameter (where you put 0) takes an alpha value (ranges from 0 to 1)
If you do 1, it’ll be at the goal, at 0, it’ll be where it started
You can do .5 to get the center between the difference

To have an alpha value, you can use a simple RunService:BindToRenderStepped() method but if you want to do something like a for loop, you can do this:

    for alpha = 1, 100 do
       alpha /= 100
       rp.CFrame = rp.CFrame:Lerp(CFrame.new(hitPosition), alpha)
    end

For example, if I had a frame at position 0,0,.5,0 and wanted to tween it to 1,0,.5,0, i’d do:

frame.Position = UDim2.new(0,0,.5,0):Lerp(UDim2.new(1,0,.5,0), alpha)
1 Like

Here’s what I currently have, and it doesn’t work:

function Mouse.HitPosition()
	local mousePos = userInput:GetMouseLocation()
	local viewportMouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
	local hitPosition

	
	
	if rayCastResult then
		hitPosition = rayCastResult.Position
		rp.CFrame = rp.CFrame:Lerp(CFrame.new(hitPosition), 0.5)
	else
		hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or RAY_DISTANCE)
	end
end


RunService:BindToRenderStepped(Mouse.HitPosition)

For :BindToRenderStepped, you’d use the delta time which is passed in the first argument

Example:

local RunService = game:GetService('RunService')
local Frame = script.Parent
local RenderStepKey = 'woooAlpha'

local Duration = 3
local elapsed = 0

local Start, End = UDim2.new(0,0,.5,0), UDim2.new(1,0,.5,0)
local function doSomething(alpha)
	local Position = Start:Lerp(End, alpha)
	Frame.Position = Position
end

RunService:BindToRenderStep(RenderStepKey, Enum.RenderPriority.Last.Value, function(dt)
	elapsed += dt
	local alpha = elapsed/Duration
	doSomething(alpha)
	if alpha >= 1 then
		RunService:UnbindFromRenderStep(RenderStepKey)
	end
end)

Elapsed will never go over Duration, and it will never go lower than 0 (because deltaTime cannot go into the minuses from the first arg of BindToRenderStepped afaik)
That means you can do et/dur (elapsedtime, duration) to get the alpha (0-1)

Now I’m back home, and I get this error: image
I tried to fix it by changing the RunService variable, but it didn’t fix it.

Because function name is supposed to be BindToRenderStep not BindToRenderStepped.

Alright, that fixed the error. But now I don’t get any result at all: nothing wrong in the output, the player doesn’t rotate to the mouse position.
My current code:

function rotateCharacter(alpha)
	local mousePos = userInput:GetMouseLocation()
	local viewportMouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
	local hitPosition
	
	
	if rayCastResult then
		hitPosition = rayCastResult.Position
		rp.CFrame = rp.CFrame:Lerp(CFrame.new(hitPosition), alpha)
	else
		hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or RAY_DISTANCE)
	end
end

RunService:BindToRenderStep(RenderStepKey, Enum.RenderPriority.Last.Value, function(dt)
	elapsed += dt
	local alpha = elapsed/Duration
	rotateCharacter(alpha)
	if alpha >= 1 then
		RunService:UnbindFromRenderStep(RenderStepKey)
	end
end)