How to create a part sway effect?


and: x.com

How does one create a swaying animation like shown in the video, I want to apply such effect to my grid placement system (see down below) but i cant find anything like this anywhere

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera

local preview = workspace:WaitForChild("Block")
local castParams = RaycastParams.new()

local function SnapToGrid(pos: Vector3)
	return Vector3.new(
		pos.X // 1,
		pos.Y // 1,
		pos.Z // 1
	) * 1
end

local function UpdatePreview()

	--| Raycast to mouse position
	local mousePos = game.UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mousePos.X, mousePos.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, castParams)

	if cast and preview then

		--| Defining position
		local snappedPos = SnapToGrid(cast.Position)
		local normalCF = CFrame.lookAlong(cast.Position, cast.Normal)
		local relativeSnapped = normalCF:PointToObjectSpace(snappedPos)
		local xVector = normalCF:VectorToWorldSpace(Vector3.xAxis * -math.sign(relativeSnapped.X))
		local yVector = normalCF:VectorToWorldSpace(Vector3.yAxis * -math.sign(relativeSnapped.Y))
		local rotation = Vector3.new(cast.Normal.Z, cast.Normal.Y, -cast.Normal.X) * 90

		local cf = CFrame.fromMatrix(snappedPos, xVector, yVector, cast.Normal)

		preview.Position = cf:PointToWorldSpace(preview.Size / 2)
	end
end

game["Run Service"].RenderStepped:Connect(function()
	UpdatePreview()
end)

if you know how to create this, please help me.

1 Like

It’s either TweenService and I think the bouncing enum direction, the spring module or a custom implementation and uses lerp