Raycast Broken?`

I am trying to make a dots projector in my game. If you want a reference point try looking at the phasmophobia dots projector.

The Issue:
My code’s raycast system seems to not be working. Not only that I need a way to constantly update that raycast. For example, if something were to walk through the dots, then the dot/laser will be updated to that position of the raycast hit.

Note:
You will see a part that is generated in teal just to show me where the ray should be shooting as well as if the distance code is still working.

I also added a wait so it would go slower so I can see what it does. This can be changed.

Code:

local origin = script.Parent

local h, k, l = origin.Position.X, origin.Position.Z, origin.Position.Y

for i = 1, 360, 10 do
    local x = h + 10 * math.cos(math.rad(i))
    local z = k + 10 * math.sin(math.rad(i))
    local y = l + 10 * math.sin(math.rad(i))

    local part = Instance.new("Part")
    part.Anchored = true
    part.Size = Vector3.new(1, 1, 1)
    part.Position = Vector3.new(x, origin.Position.Y, z)
    part.Parent = workspace
    part.BrickColor = BrickColor.new("Teal")
    part.Transparency = 0.5
    part.CanCollide = false
    part.CanQuery = false

    local Laser = Instance.new("Part")
    Laser.Anchored = true
    Laser.Size = Vector3.new(0.1, 0.1, 0.1)
    Laser.Parent = workspace
    Laser.BrickColor = BrickColor.new("Lime green")
    Laser.Transparency = 0
    Laser.CanCollide = false
    Laser.CanQuery = false

    local Result = workspace:Raycast(origin.Position, part.Position)

    if Result then
        Laser.Position = Result.Position
    end

    wait(0.3)
end

PLEASE HELP!!!

For a ray to hit something you need to not set CanQuery to false so you can remove that line when creating the “part” object. Also you need to provide a direction and a distance in that direction. Try this:

local origin = script.Parent
local distance = 20

local h, k, l = origin.Position.X, origin.Position.Z, origin.Position.Y

for i = 1, 360, 10 do
	local x = h + 10 * math.cos(math.rad(i))
	local z = k + 10 * math.sin(math.rad(i))
	local y = l + 10 * math.sin(math.rad(i))

	local part = Instance.new("Part")
	part.Anchored = true
	part.Size = Vector3.new(1, 1, 1)
	part.Position = Vector3.new(x, origin.Position.Y, z)
	part.Parent = workspace
	part.BrickColor = BrickColor.new("Teal")
	part.Transparency = 0.5
	part.CanCollide = false

	local Laser = Instance.new("Part")
	Laser.Anchored = true
	Laser.Size = Vector3.new(0.1, 0.1, 0.1)
	Laser.Parent = workspace
	Laser.BrickColor = BrickColor.new("Lime green")
	Laser.Transparency = 0
	Laser.CanCollide = false
	Laser.CanQuery = false
	Laser.Name = "Laser"
	
	local rayOrigin = origin.Position
	local rayDestination = part.Position

	local rayDirection = rayDestination - rayOrigin

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection * distance)
		
	if raycastResult then
		Laser.Position = raycastResult.Position
	end

	wait(0.3)
end
2 Likes

Pretty easy solution and I missed it by a smidge. Thanks for the help!

The fix for that would be to change this line:

local rayDirection = (rayDestination - rayOrigin).Unit

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.