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