# How to lerp my part opposing movement direction

So basically, I made a placement system and to make it look cooler I’m trying to add some type of air resistance when moving a part to the right, left, forward or backward and so it would lerp accordingly and then when the movement is over it lerps back to original orientation.

1 Like

Hello! Here is solution:

``````local part = workspace.Part -- your path to part

local OppositeDirection = -part.AssemblyLinearVelocity

part:PivotTo(part:GetPivot():Lerp(CFrame.new(OppositeDirection), 1/100)) -- you can replace increment with your own
``````

I hope it helps

1 Like

Hello!

Thank you so much for your reply, it appears i didnt quite make sense but i’ll try to explain better.
Unfortunately i dont have an exemple to provide but heres what im trying to achieve

excuse my poor drawing skills but basically as the part follows my mouse on a renderstep loop, i want it so when moved in a certain direction it slightly tilts towards it similarly to that drawing, and then as i stop moving my mouse, its back to its original non tilted state so it adds a “almost going to fall” effect

1 Like

You must provide some code, because it’s very specific task. And if solution I provided before doesn’t help, I need to see the code. Lerp is function to calculate point between two points (In roblox is CFrames and Vectors) with given increment, here is instance of using it:

``````--[[Cframe/Vector]]:Lerp(Cframe/Vector, Alpha: number)

print(Vector3.new(10, 2, 5):Lerp(Vector3.zero, 1/2)) --> 5, 1, 2.5
print(Vector3.new(1, 50, 6):Lerp(Vector3.new(2, 25, 3), 1/2)) --> 1.5, 37.5, 4.5
``````

Then, to get moving direction we can use Velocity , AssemblyLinearVelocity (Velocity is deprecated), to make it opposite we have to make it negative, and then initialize lerp.

2 Likes

Hello again!
Thank you again for your reply but I seem to be misunderstood again, here’s my placement system :
https://gyazo.com/ba1338673b6169f5efab4d0154ee5034

I have been scripting for over 4 years and i’m quite acquainted with everything, the reason i cant make usage of velocity is due to the position being set on a loop and so its velocity wont really be affected and so that isnt an option, and so if u have another analogy of a way I can make this rotation tilting while moving the object in specific directions, btw I’m only trying to tilt the orientation and not the position, so whether its lerping or tweening, aslong as it works.

1 Like

How ‘rotation tilting’ will look like? There’s can be alot of ways.

1 Like

So basically similarly to the paint image i sent, if it moves forward, it will slightly tilt forward using a tween/lerp, if its right then it tilts to the right, if the mouse is moving to the left, then tilt towards the left

1 Like

It does make sense now, yes it’s very interesting question. Here is example of solution:

1. You have to calculate mouse moving direciton
(nvm I saw the post with solution — post)
2. You have to make a variable to handle direction value,
And after a bit of modifying, code will look like:
``````local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local lastMouseX, lastMouseY = mouse.X, mouse.Y
local TiltAngle = 10 -- adjust it to change the tilt angle

mouse.Move:Connect(function()
local Direction : CFrame
if math.abs(mouse.X - lastMouseX) > math.abs(mouse.Y - lastMouseY) then
if mouse.X - lastMouseX > 0 then
Direction = CFrame.Angles(rad(-TiltAngle), 0, 0)
else
Direction = CFrame.Angles(rad(TiltAngle), 0, 0)
end
else
if mouse.Y - lastMouseY > 0 then
Direction = CFrame.Angles(0, 0, rad(-TiltAngle))
else
Direction = CFrame.Angles(0, 0, rad(TiltAngle))
end
end
lastMouseX, lastMouseY = mouse.X, mouse.Y
end)
``````

( I don’t guarantee this code will work 100% as you expect, it’s just I’m just showing the instance of how I would solve this problem )
3. Now you have direction as CFrame so you can easily do that.
You need to define two new variables:
i (number) — which is increment for lerp and:
OldDirection (blank cframe) — to reset i if mouse direction was changed.
Then because of you having render step following code will similiar to:

``````

local RunService = game:GetService('RunService')
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local lastMouseX, lastMouseY = mouse.X, mouse.Y
local TiltAngle = 10 -- adjust it to change the tilt angle
local OldDirection = CFrame.Angles(0, 0, 0)

local i = 1
local lerp = function(a: number, b: number, t: number): (number) -- lerp for to lerping variable i in loop, for lerping direction.
return a + (b - a) * t
end
function CalculateDirection(): (CFrame, boolean)
local Direction : CFrame
if math.abs(mouse.X - lastMouseX) > math.abs(mouse.Y - lastMouseY) then
if mouse.X - lastMouseX > 0 then
Direction = CFrame.Angles(rad(-TiltAngle), 0, 0)
else
Direction = CFrame.Angles(rad(TiltAngle), 0, 0)
end
else
if mouse.Y - lastMouseY > 0 then
Direction = CFrame.Angles(0, 0, rad(-TiltAngle))
else
Direction = CFrame.Angles(0, 0, rad(TiltAngle))
end
end
lastMouseX, lastMouseY = mouse.X, mouse.Y
local isChanged =

OldDirection = Direction
return Direction, OldDirection ~= Direction
end
RunService.RenderStepped:Connect(function(dt: number)
local Direction, IsChanged = CalculateDirection()
if IsChanged then
i = 0
end
i = lerp(i, 1, 1/100)
-- your lerp logic here
end)

``````

I hope it was a bit more helpful!

2 Likes

Hello!

thank you again for the effort ure putting in to help out, this is indeed very helpful, the only issue is that if the mouse is moving slightly diagonally then it would prompt right and forward tilt at the same time i suppose and that would make it seem rather buggy, any ideas on how i can solve that?
possibly by prioritizing one of the 2?

Again thank you a lot!

1 Like

You’re welcome! Hmmm, yes I see. Here is solution:

• I added a constant ‘maxAccuracy’. It do checks if mouse moving direction Y more than maxAccuracy value, then don’t do the check for horizontal directions.
``````local RunService = game:GetService('RunService')
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local lastMouseX, lastMouseY = mouse.X, mouse.Y
local TiltAngle = 10 -- adjust it to change the tilt angle
local OldDirection = CFrame.Angles(0, 0, 0)

local i = 1
local maxAccuracy = 5
local lerp = function(a: number, b: number, t: number): (number) -- lerp for to lerping variable i in loop, for lerping direction.
return a + (b - a) * t
end
function CalculateDirection(): (CFrame, boolean)
local Direction : CFrame
warn(math.abs(mouse.X - lastMouseX), math.abs(mouse.Y - lastMouseY))
if math.abs(mouse.X - lastMouseX) > math.abs(mouse.Y - lastMouseY) and math.abs(mouse.Y - lastMouseY) < maxAccuracy then
if mouse.X - lastMouseX > 0 then
Direction = CFrame.Angles(rad(-TiltAngle), 0, 0)
print('right')
else
print('left')
Direction = CFrame.Angles(rad(TiltAngle), 0, 0)
end
else
if mouse.Y - lastMouseY > 0 then
print('up')
Direction = CFrame.Angles(0, 0, rad(-TiltAngle))
else
print('down')
Direction = CFrame.Angles(0, 0, rad(TiltAngle))
end
end
lastMouseX, lastMouseY = mouse.X, mouse.Y
OldDirection = Direction
return Direction, OldDirection ~= Direction
end
RunService.RenderStepped:Connect(function(dt: number)
local Direction, IsChanged = CalculateDirection()
if IsChanged then
i = 0
end
i = lerp(i, 1, 1/100)
-- your lerp logic here
end)
``````
2 Likes

hey!
excuse my latency on the reply but i finally managed to get it to work, take a look :
https://gyazo.com/62e5df42ca5c364931a9ef6cf10fa7b3

I used cframe values and tweened them instead because it was quite annoying with lerping and tweaked everything, even tho the code was completely altered and brand new, i definitely used ur code as a template and so i’m very thankful!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.