Trying to make a more smooth fishing rod cast

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)

Heres an example of how it looks like rn:

3 Likes

i’m guessing this is handled on the server, you should handle it on the client so there’s no replication delay

What does “smooth” mean in your case? The performance of the code? Or the movement of the bait part?

Movement on the bait part and would be nice to know how to better write this code.

Cant i just set network part owner to nil?

Setting the network ownership to nil means that it will be handled by the server only.

Yeah and it would be smoother, no?

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)

Can you give me an example of how it would look like on my code? Thanks.

Put it in a local script, that way it’ll run on the client :stuck_out_tongue:

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.

How would other players see it?

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

yeah that aint gonna work for me, can you maybe do an example code for mine, if not its fine ill just stick with this

It’s way too warm for coding today, but maybe later.

2 Likes

alright thank you so much for the help you already provided me

1 Like

I do not get it what does “smooth” mean in your case? However, I will still try to help you!

  1. Use a Bezier curve for smoother motion

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

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.