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
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)