local function createHookingPointRight(player)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = { player.Character }
raycastParams.IgnoreWater = true
local mouse = player:GetMouse()
local startTime = os.clock()
local elapsedTime = 0
repeat
local raycastResult = workspace:Raycast(player.Character.HumanoidRootPart.Position, mouse.UnitRay.Direction, raycastParams)
if raycastResult then
local hookingPointRight = raycastResult.Position
HookRight.Value = "True"
print("Found hooking point at:", hookingPointRight)
return
end
elapsedTime = os.clock() - startTime
print("Still searching for hooking point...")
wait()
until elapsedTime >= 2
print("No hooking point found within the time limit.")
end
I am trying to make constant raycast detection that if no result is found, as long as function is being ran(holding E), send raycast to where mouse cursor is pointing and if it found a result, stop
But currently its only working by player’s character direction and not where mouse cursor is looking at
You would fire to the mouses position by working out where its direction and distance is, this is my current ray module i tend to use for basic rays.
local RayModule = {}
function RayModule.FireToPosition(Origin,Pos)
if typeof(Origin) == "CFrame" then
Origin = Vector3.new(Origin.X,Origin.Y,Origin.Z)
end
if typeof(Pos) == "CFrame" then
Pos = Vector3.new(Pos.X,Pos.Y,Pos.Z)
end
local CF = CFrame.lookAt(Origin,Pos)
local Direction = CF.LookVector
local Distance = (Pos - Origin).Magnitude * Direction
-- Fire Ray
return RayModule.FireRay(Origin,Distance)
end
function RayModule.FireRay(Origin,Direction)
local Result = game.Workspace:Raycast(Origin,Direction)
-- RaycastResult.Instance The BasePart or Terrain cell that the ray intersected.
-- RaycastResult.Position The world space point at which the intersection occurred
-- RaycastResult.Material The Material at the intersection point
-- RaycastResult.Normal The normal vector of the intersected face.
-- RaycastResult.Distance
return Result
end
--local raycastParams = RaycastParams.new()
--raycastParams.FilterDescendantsInstances = {caster.Parent}
--raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
--local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
--return Result
return RayModule
You’ve got the right idea using mouse.UnitRay.Direction. In order for the raycast to be effective though, you should multiply it by a number as this will determine the length of your ray.
Example:
Blockquote
local raycastResult = workspace:Raycast(player.Character.HumanoidRootPart.Position, mouse.UnitRay.Direction * NUMBER_HERE, raycastParams)