I’m trying to make a projectile using RayCast however when I try to spawn a part on my mouse just to check if the Ray is working properly, it spawns in a somewhat random spot as you can see in this video,
My Code:
local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRP = char:WaitForChild("HumanoidRootPart")
local RS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local debounce = false
local CD = .1
local tpRemote = RS.Remotes.Teleport
local mouse = plr:GetMouse()
local KEY = Enum.KeyCode.V
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
local function throwKunai()
local kunai = RS.FX.RaijinKunai:Clone()
local rayOrigin = humRP.Position
local rayDirection = mouse.Hit.Position
local raycastResult = game.Workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult == nil then return
end
local objHit = raycastResult.Instance
local spawnPart = Instance.new("Part")
spawnPart.Position = raycastResult.Position
spawnPart.Parent = workspace.Map.Ignore
print("Pressed V")
end
UIS.InputBegan:Connect(function(inp, gpe)
if gpe then return end
if inp.KeyCode == KEY and not debounce then
debounce = true
throwKunai()
task.wait(CD)
debounce = false
end
end)