How would I make a 'sway effect' for a tower defence game?

I want to recreate this effect from tower defence simulator to make my towers bounce / sway

I have got the delta, but i do not have any clue on how I would make it move in that way.

I found this post Attempting to do a "sway" effect however, it is very complicated for my tiny brain. I am very good at other things, just not so good with springs. Below is what i have written

local function TowerPlacing(TowerModel, RayCast)
--My mouse raycast
	RayCast = MouseRayCast({TowerModel, workspace.Enemies, workspace.Towers, PlayerModels})
--Avoid bugs and get the Delta
	if Previous then
		Delta = (Previous - TowerModel.PrimaryPart.Position)
	end
--Move the part
	if RayCast then
		TowerModel.PrimaryPart.CFrame = CFrame.new(RayCast.Position + Vector3.new(0, TowerDataBase.Placing[1].YOffset, 0)) * CFrame.Angles(0, math.rad(TowerDataBase.Placing[1].Rotation), 0)
	end
--Move the range and stuff
	Range.CFrame = CFrame.new(Vector3.new(TowerModel.PrimaryPart.Position.X, RangeY, TowerModel.PrimaryPart.Position.Z)) * CFrame.Angles(0, 0, math.rad(90))
	Previous = TowerModel.PrimaryPart.Position
--ignore whats under this
	RayCheck(TowerInfoDataBase[TowerModel.Name].Range)
end
9 Likes

you would use two springs, one for the position of the tower, and one for the angle of the tower. the angle changes based on the direction it is moving in, which i think is (newPosition - lastPosition).Unit * math.min((newPosition - lastPosition).Magnitide, 15)
15 is the maximum angle

locla tower = workspace.Tower

local springAngular = spring.New()
local springPositional = spring.New()

-- set dampening and speed to your liking.

RunService.Heartbeat:Connect(function(dt)
      springAngular:Update(dt)
      springPositional:Update(dt)

      springAngular.Target = (newPosition - lastPosition).Unit *  math.min((newPosition - lastPosition).Magnitide, 15)
      springPositional.Target = mouseHit.Position --  raycast where mouse is

      tower.CFrame = CFrame.new(springPositional) * CFrame.Angles(math.rad(springAngular.X), math.rad(springAngular.Y), math.rad(springAngular.Z))
end)
7 Likes

Where is that spring.new() coming from?
Which module.

5 Likes

you need to find one
use nevermores

4 Likes

Update is not a valid member of spring?

5 Likes

change my code to work. my code doesnt use an actual spring module but a representation of one

3 Likes

what would i change update to though? It doesnt give me any recomendations

3 Likes

my bad, if you’re using nevermore change ‘Update’ to ‘TimeSkip’ and spring.Target = x to spring:Impulse(x) for both springs

4 Likes

ReplicatedStorage.Modules.Effects.Spring:63: attempt to perform arithmetic (add) on number and Vector3 - Client - Spring:63

1 Like