Trying to make a smoother fishing rod cast, i made this code in like 30 seconds but i dont know a better way to do it, anyone got any recommendations on what i should do / try?
local Tool = script.Parent
local Casted = false
local Cast = Tool["Fishing Rod Cast"]
local function lerp(a,b,t)
return a+(b-a)*t
end
Tool.Activated:Connect(function()
if not Casted then
Casted = true
local track = Tool.Parent.Humanoid.Animator:LoadAnimation(game.ReplicatedStorage.Cast)
track:Play()
task.wait(.7)
Cast:Play()
local BallClone = game.ReplicatedStorage.Ball:Clone()
BallClone.Beam.Attachment0 = Tool.Ball.Attachment
BallClone.Beam.Attachment1 = BallClone.Attachment
BallClone.Name = "BaitClone"
BallClone.CFrame = Tool.Ball.CFrame
BallClone.Parent = Tool
local StartPos = Tool.Ball.Position
local EndPoint = Tool.Parent.PrimaryPart.CFrame * CFrame.new(0, -20, -60)
local MidPoint = Instance.new("Part")
MidPoint.Position = (StartPos+EndPoint.Position)/2 + Vector3.new(0,40,0)
MidPoint.Anchored = true
MidPoint.Size = Vector3.new(2,2,2)
MidPoint.Material = Enum.Material.Neon
MidPoint.Name = "Midpoint"
MidPoint.Parent = Tool
for Index = 0, 100, 1.5 do
local t = Index/100
local l1 = lerp(StartPos, MidPoint.Position, t)
local l2 = lerp(MidPoint.Position, EndPoint.Position, t)
local quad = lerp(l1, l2, t)
BallClone.Position = quad
task.wait()
end
elseif Casted then
Casted = false
local BallClone = Tool:FindFirstChild("BaitClone")
BallClone:Destroy()
end
end)
You should do tweening of CFrames on the client, not the server. That means the client will perform the calculations, instead of the server (which takes more time, due to network latency)
No. The reason it isn’t smooth is because you’re setting its position in a for loop that waits with task.wait(). Network ownership wouldn’t change that. What you could use instead is just roblox’s physics. You could set the CanCollide property of the bait to nil (or use collision groups) and set the velocity of the bait.
They wouldn’t. But that’s a different question entirely. You could replicate it to the server, or have the server send a message to all clients telling them that fishingpoleX will now cast it’s bobber in direction Y, with velocity Z. And then the clients can just run the code. This you would have to use a remote event for.
i know what a remote event is, but if i make the whole script of a bezier curve inside of client, how would i then use a remote to transfer what i already did in client in server?
You could have code on the client for simulating the fishing rod. And then when clicking, send to the server the player, the rod, the direction and the velocity. Then send back to every client, start simulating from player X, their rod, and simulate towards direction with velocity. This is not super easy, btw, especially and could be done in different ways than just this one.
Also, you’d need to ensure the result is the same for everyone. It may just be simpler to do have the server control the position of the bob, and then have the clients interpolate (lerp) the CFrames in between
I do not get it what does “smooth” mean in your case? However, I will still try to help you!
Use a Bezier curve for smoother motion
Consider cleanup on termination If the tool activation is terminated before completion (e.g., due to player death or the tool being unequipped), your script currently leaves “BaitClone” behind. I believe that you would add that later but just in case you forgot
local Tool = script.Parent
local Casted = false
local Cast = Tool["Fishing Rod Cast"]
local function lerp(a,b,t)
return a+(b-a)*t
end
local function CastFishingRod()
Casted = true
local track = Tool.Parent.Humanoid.Animator:LoadAnimation(game.ReplicatedStorage.Cast)
track:Play()
task.wait(.7)
Cast:Play()
local BallClone = game.ReplicatedStorage.Ball:Clone()
BallClone.Beam.Attachment0 = Tool.Ball.Attachment
BallClone.Beam.Attachment1 = BallClone.Attachment
BallClone.Name = "BaitClone"
BallClone.CFrame = Tool.Ball.CFrame
BallClone.Parent = Tool
local StartPos = Tool.Ball.Position
local EndPoint = Tool.Parent.PrimaryPart.CFrame * CFrame.new(0, -20, -60)
local MidPoint = (StartPos + EndPoint.Position)/2 + Vector3.new(0,40,0) -- calculate the midpoint
for Index = 0, 100, 1.5 do
local t = Index/100
local l1 = lerp(StartPos, MidPoint, t)
local l2 = lerp(MidPoint, EndPoint.Position, t)
local quad = lerp(l1, l2, t)
BallClone.Position = quad
task.wait()
end
end
local function ResetFishingRod()
Casted = false
local BallClone = Tool:FindFirstChild("BaitClone")
if BallClone then
BallClone:Destroy()
end
end
Tool.Activated:Connect(function()
if not Casted then
CastFishingRod()
else
ResetFishingRod()
end
end)
Tool.Unequipped:Connect(ResetFishingRod)