I’ve been working on a fishing system. When a player clicks, it casts the line to where the mouse was clicked. The issue is the casted ray goes to the other direction instead of where the mouse clicks. Here is photo of what happens
As you can see, the white translucent ‘string’ that appears follows the green line instead of what I expected, which was the yellow line. The yellow line is the direction and position of where it is supposed to go when water is clicked.
How would I go about to make it so that it would go in the direction of the mouse and not some other direction?
Current code:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character
local head = character:FindFirstChild("Head")
local humanoid = character:WaitForChild("Humanoid")
local animWield = script.Parent.Animation
local wieldRod = humanoid:LoadAnimation(animWield)
local animThrow = script.Parent.Animation2
local throwRod = humanoid:LoadAnimation(animThrow)
local animIdle = script.Parent.Animation3
local idleRod = humanoid:LoadAnimation(animIdle)
local animReel = script.Parent.Animation4
local reedRod = humanoid:LoadAnimation(animReel)
local isFishing = false
local caught = false
local count = 0
local waitBeforeReel = false
local topPart = script.Parent.Top
rodRay = Ray.new(topPart.Position + Vector3.new(0, 10, 0), Vector3.new(0,-10,0)*100)
stringRay = Ray.new(topPart.CFrame.p, (mouse.Hit.p -topPart.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(stringRay, player.Character, true, false)
local objecthit, hitposition, normal, material = workspace:FindPartOnRay(rodRay, topPart, true,false)
function createString()
stringPart = Instance.new("Part")
--properties
stringPart.Name = "StringPart"
stringPart.BrickColor = BrickColor.new("White")
stringPart.FormFactor = "Custom"
stringPart.Material = "SmoothPlastic"
stringPart.Transparency = 0.5
stringPart.Anchored = true
stringPart.Locked = true
stringPart.CanCollide = false
stringPart.Parent = workspace
--calculate length of the part
local distance = (topPart.CFrame.p - position).magnitude
stringPart.Size = Vector3.new(0.3, 0.3, distance)
stringPart.CFrame = CFrame.new(topPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
end
script.Parent.Activated:Connect(function()
if objecthit.Name == "Terrain" and isFishing == false and count == 0 then --checks if the clicked area was a terrain region
waitBeforeReel = true --prevents the player from reeling
print("water")
count = 1
isFishing = true
game.ReplicatedStorage.changeStats:FireServer(0,humanoid.WalkSpeed)
wieldRod:Stop()
throwRod:Play()
wait(1)
createString()
idleRod:Play()
humanoid.WalkSpeed = 12
print("caged")
local timing = math.random(10,90) --timing before the fish comes
print(tostring(timing))
wait(5)
waitBeforeReel = false --allowing the reel anytime(if player wants to quit)
wait(timing)
print("fishing...")
--planning to write some function here later
end
if isFishing == true and count == 1 and caught == false and waitBeforeReel == false then --if the player wants to quit fishing
idleRod:Stop()
wait(0.1)
reedRod:Play()
wait(2)
wieldRod:Play()
isFishing = false
count = 0
humanoid.WalkSpeed = 12
workspace:FindFirstChild("StringPart"):Destroy() --destroys the 'beam'
end
end)
