Strange Raycasting Behaviour

  1. What do you want to achieve? A raycast that gets the position of where the raycast hit.

  2. What is the issue? The raycasting works fine on everything except for the baseplate, the impact particle doesnt even spawn.

  3. What solutions have you tried so far? I’ve looked over the API and watched a few videos. I would use the depreciated version which Im more familiar with but I want to learn the new way.

here’s the script that handles the raycasting

	connection = Blast.Touched:Connect(function(touched)
		connection:Disconnect()
		local rayOrigin = Blast.Position
		local rayDirection = Position

		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {Character:GetDescendants()}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

		task.spawn(function()
			if raycastResult then
				local HitPosition = raycastResult.Position
				
				local Impact = Effects["Psionic Blast Impact"]:Clone()
				Impact.Parent = workspace.DebrisFolder
				Impact.Position = HitPosition

				for i, v in pairs(Impact:GetDescendants()) do
					if v:IsA("ParticleEmitter") then
						v.Enabled = true
					end
					if v:IsA("PointLight") then
						TweenService:Create(v, TweenInfo.new(0.6), {Brightness = 2}):Play()
					end
				end

				task.wait(0.4)

				for i, v in pairs(Impact:GetDescendants()) do
					if v:IsA("ParticleEmitter") then
						v.Enabled = false
					end
					if v:IsA("PointLight") then
						TweenService:Create(v, TweenInfo.new(0.6), {Brightness = 0}):Play()
					end
				end

				wait(10)

				Impact:Destroy()
			end
		end)

all help is greatly appreciated

what is the Position variable?

the mouse position from the client

Figured out the problem for anyone else having this issue.

just create a CFrame withe the ray origin facing the ray direction and gets its look vector * 50 (doesnt need to be 50) it just moves the direction a bit further to allow the ray to move a bit further to find the part

local rayDirection = (Position - rayOrigin).Unit
...
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * n, raycastParams)

Where ‘n’ is the ray’s length (in studs).

1 Like