Following ego moose’s tutorial on springs I tried making one, it works somewhat but does not oscillate(meaning goes over the target point to go back and reach it) and I don’t know why I even copied his code word for word.
here’s my code:
local script;
local part = workspace.FreePart;
local mouse = game.Players.LocalPlayer:GetMouse();
mouse.TargetFilter = part;
local springModule = require(script:WaitForChild("spring"));
local spring = springModule.new(part.CFrame.p, Vector3.new(), mouse.Hit.p);
spring.k = 0.05;
spring.friciton = .1;
game:GetService("RunService").RenderStepped:Connect(function()
spring.target = mouse.Hit.p;
spring:Update();
part.CFrame = CFrame.new(spring.position);
end);
module script;
local spring = {};
function spring.new(position, velocity, target)
local self = setmetatable({}, {__index = spring});
self.position = position
self.velocity = velocity
self.target = target
self.k = 1;
self.friction = 1;
return self;
end;
function spring:Update()
local d = (self.target - self.position);
local f = d * self.k;
self.velocity = (self.velocity * (1 - self.friction)) + f;
self.position = self.position + self.velocity;
end;
return spring;