Im trying to cast a ray in front of the character head but for some reasons it casting to a wrong position
local RS = game:GetService("RunService")
local Character = script.Parent
local Hum = Character:WaitForChild("Humanoid")
local Hrp = Character:WaitForChild("HumanoidRootPart")
local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.FilterDescendantsInstances = {Character}
RS.RenderStepped:Connect(function()
local origin = Character.Head.Position
local position = Character.Head.CFrame.LookVector * 5
local direction = position - origin
local result = workspace:Raycast(origin, direction, CastParams)
local p = Instance.new("Part") --Creating visual
p.Parent = workspace
p.CanCollide = false
p.Anchored = true
p.Position = position
p.Size = Vector3.new(1,1,1)
game.Debris:AddItem(p, 0.01)
if result and result.Instance then
--print(result.Instance.Name)
end
end)
your direction variable is not needed. position is already enough
local RS = game:GetService("RunService")
local Character = script.Parent
local Hum = Character:WaitForChild("Humanoid")
local Hrp = Character:WaitForChild("HumanoidRootPart")
local CastParams = RaycastParams.new()
CastParams.IgnoreWater = true
CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.FilterDescendantsInstances = {Character}
RS.RenderStepped:Connect(function()
local origin = Character.Head.Position
local position = Character.Head.CFrame.LookVector * 5
local direction = position - origin
local result = workspace:Raycast(origin, position, CastParams)
if result then
local p = Instance.new("Part")
p.Parent = workspace
p.CanCollide = false
p.Anchored = true
p.Position = result.Position
p.Size = Vector3.new(1,1,1)
task.wait()
p:Destroy()
end
end)