Code Simplification & Physics

Currently I am making a fishing game that uses the players cursor movement and the speed of the end of the fishing rod as a way to cast. I would like feedback and help on how to continue to develop the rods functions.

First I just want more feedback on my code. I think as is that it is very sloppy and I want to make it more concise. Additionally if you could explain how / why its better for a change to be there. it would be appreciated because I am new dev.

--Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

--Runservice functions
local heartbeatConn = nil

--arm variables
local active = false
local plr = Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()	
local armOffset = char.Torso.CFrame:Inverse() * char["Right Arm"].CFrame
local armWeld = nil

--Launch variables
local Rod = script.Parent
local floatSpeed = nil
local sampleHook = game.ReplicatedStorage.Assets.Rod.Hook
local hook = nil
local rope = Instance.new("RopeConstraint")
local ready = false

local activehook = false
local bindable = game.ReplicatedStorage.RemoteEvents.TrackBindable

Rod.Equipped:Connect(function()
	--Create thing
	armWeld = Instance.new("Weld")
	armWeld.Part0 = char.Torso
	armWeld.Part1 = char["Right Arm"]
	armWeld.C0 = armOffset
	armWeld.Parent = char

	-- Connect Heartbeat
	heartbeatConn = RunService.Heartbeat:Connect(function()
		if active and armWeld then
			local mouseVector = Vector3.new(mouse.Hit.X, mouse.Hit.Y, 6.5)
			local cframe = CFrame.new(char.Torso.Position, mouseVector) * CFrame.Angles(math.rad(90), 0, 0)
			local Location = armOffset * char.Torso.CFrame:toObjectSpace(cframe)
			if Location.XVector.X < 0 then
				local cframe = CFrame.new(char.Torso.Position, mouseVector) * CFrame.Angles(math.rad(90), math.rad(180), 0)
				local Location = armOffset * char.Torso.CFrame:toObjectSpace(cframe)
				armWeld.C0=Location
			else
				armWeld.C0 = Location
			end
			speedCalc()
		end
	end)
end)

function speedCalc()
	local x1, y1 = Rod.Tip.Position.X, Rod.Tip.Position.Y
	local p0 = Vector2.new(x1, y1)
	task.wait(.05)
	local x2, y2 = Rod.Tip.Position.X, Rod.Tip.Position.Y
	local p1 = Vector2.new(x2, y2)
	local p = (p0-p1) 
	local c2 = p.X^2 + p.Y^2
	local distance = math.sqrt(c2)
	local velocity = (distance/0.05)
	if velocity > 10 then
		if velocity > 150 then
		 floatSpeed = 150
		else
			floatSpeed = velocity
		end
		ready = true
	else
		ready = false
	end
end

function hookmake()
	--spawning hook
	hook = sampleHook:Clone()
	activehook = true
	bindable:Fire(activehook, hook)
	launcher = hook.Launcher
	resistance = hook.Resistance
	resistance.Force = Vector3.new(0,0,0)
	rope = Instance.new("RopeConstraint")
	hook.Parent = workspace
	rope.Attachment0 = Rod.Rod.Point0
	rope.Attachment1 = hook.HookMesh.Point1
	rope.Visible = true
	rope.Color = BrickColor.White()
	rope.Thickness = .025
	rope.Parent = Rod.Rod.Point0
	hooklaunch()
end


function hooklaunch()
	--Launching hook
	hook.CFrame = CFrame.lookAlong(Rod.Tip.Position, Rod.Tip.CFrame.LookVector)
	local speed = math.ceil(floatSpeed/3)
	rope.Length = 3
	launcher.Force = CFrame.Angles(0, 0, 0).RightVector * speed
	for i =1, speed do
		task.wait(.01)
		local distance = (hook.Position - Rod.Tip.Position).Magnitude
		rope.Length = distance+speed/10
		resistance.Force += Vector3.new(1,0,0)
	end
end

Rod.Activated:Connect(function()
	active = true
	if hook and rope then
		activehook=false
		hook:Destroy()
		bindable:Fire(activehook)
		rope:Destroy()
	end 
end)

Rod.Deactivated:Connect(function()
	active = false
	if hook and string then hook:Destroy(); rope:Destroy() end
	if ready == true then hookmake() end
	repeat task.wait() until activehook == false
	if armWeld then
		armWeld.C0 = armOffset
	end
end)

Rod.Unequipped:Connect(function()
	active = false
	if heartbeatConn then
		heartbeatConn:Disconnect()
		heartbeatConn = nil
	end
	if armWeld then
		armWeld:Destroy()
		armWeld = nil
	end
	if hook and rope then
		hook:Destroy()
		activehook = false
		bindable:Fire(activehook)
		rope:Destroy()
	end
end)

Second i want some direction on how I should add water resistance to slow the descent of the hook.

Lastly I would like to know how to align the hook to a specific axis currently the game is a sort of 2.5D game. I have tried creating a plane constraint on the cloned hook but once cast it just freaks out.

(Please excuse the poor quality)

External Media
1 Like

perhaps you could use linear velocity like here or perhaps you could set Workspace.FluidForces = Experimental and increase Workspace.AirDensity

once the hook is in the water, maybe you could allow small adjustments to the position and decrease the MouseDeltaSensitivity to give a feel of resistance

Not exactly what I was looking for but it still lead me into the right direction. After bashing my head against a wall for four days trying to figure stuff out I have a semi working idea

Oh, sorry. If you would like, we can discuss other possible solutions to help you achieve exactly what you want.


While harsh, this is the best method to think of any ideas. This physical trauma creates a psychological reinforcement that failure to come up with brilliant ideas results in pain and punishment. This mentally tunes your brain to work harder, more efficiently, and more productively, as the constant fear of this self-inflicted punishment drives one to obtain results without delay


Alright, so what exactly were you looking for? I shall help.

[spoiler]

Well, it can be really helpful for these things

By the way, while using AI for everything in your game (and in life) is a shameful thing to do, AI can be really helpful for tasks like this where you have a working prototype but just need some help cleaning it up and fixing some minor issues. I would recommend you just ask ChatGPT or Co-Pilot to help clean up your code.

[/spoiler]

It is all good. At least for now :heart_on_fire:

Are you sure? I insist!

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