So I am learning raycasting and at line 13 why is the ray not being casted towards the torso of the r6 player? The direction it is being fired at is wrong, why is this and how to fix it?
Line 13 is when i make the ray (local MyRay = Ray.new…)
local MyRay = Ray.new(script.Parent.Position, Vector3.new(0,50,0))
while wait() do
local target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and human.Health > 0 then
if (script.Parent.Position - torso.Position).magnitude < 100 then
local MyRay = Ray.new(script.Parent.Position, (script.Parent.Position - torso.Position).Unit * 500) -- line 13
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(MyRay, {script.Parent})
if hit == torso then
target = torso
end
end
end
end
if target then
local torso = target
script.Parent.CFrame = CFrame.new(script.Parent.Position, torso.Position)
local bullet = game.ReplicatedStorage:WaitForChild("Bullet"):Clone()
bullet.Position = script.Parent.Position
bullet.Parent = workspace
bullet.Velocity = script.Parent.CFrame.LookVector * 500
bullet.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
human:TakeDamage(10)
end
end)
end
end