A really weird Raycasting glitch. How can I fix this?

Hi, Somehow I have a pretty weird bug with Raycasting. I am trying to get the position of a collision between 2 Parts. After that I want to place a ParticleEmitter. And how I said, I have a bug that I dont
understand and I also dont know how to fix it.

Here’s a video that explains it:

Normaly it’s supposed to place the particles at the area where the knife just hit the wall.

These are the lines of the script with the Raycasting (Serverscript inside the knife):

script.Parent.Handle.Touched:Connect(function(Touched)
...
local RayCastParams = RaycastParams.new()
				local rayOrigin = Touched.Position
				local rayDirection = script.Parent.Handle.Position
				local raycastResult = game.Workspace:Raycast(rayOrigin, rayDirection, RayCastParams)
...
ParticleClone.Position = raycastResult.Position
end)

Does anyone know how I can fix this? To be honest I never used Raycasting before so I dont really know much about it. I will be checking every message in this post one by one and see if the solution will work.
Also I might wont answer until tomorrow because it is late.

I am thanking anyone who tries to help me.

Try this:

script.Parent.Handle.Touched:Connect(function(Touched)
   local RayCastParams = RaycastParams.new()
   local rayOrigin = Touched.Position
   local rayGoal = script.Parent.Handle.Position
   local raycastResult = game.Workspace:Raycast(rayOrigin, rayGoal - rayOrigin, RayCastParams)

   if raycastResult then
         ParticleClone.Position = raycastResult.Position
   end
end)

But will not be better to make a ray from the knife foward its cframe, and if it hits something add the particle?

2 Likes

This isn’t a glitch, at least not on Roblox’s side. You cast a ray from the knife towards the part it touches, but you neither filter the part, nor do you actually create the ray correctly. The moment the knife touches the part, what tells you that the knife isn’t inside the part? In that case, the ray will start from inside the part and never detect any faces. That’s why it just casts the ray into some random direction you pointed out in your video.

I also question whether there’s any point in using raycasting here. If you just want to cast a bunch of particles on hit, why don’t you just cast them from the tip of the knife?

1 Like

I already tried to place a little part with a ParticleEmitter inside the knife. But that would also cause a few problems. If I wanna make a longer weapon and it hits a part the player isn’t going to see the particles. With Raycasting I am probably able to fix this.

The problem is that you need to figure out what direction to cast the ray in, as well as where to cast it from. If the part you hit is big, the direction will be completely off as it’s center can be far from the point you touch. I’d do something like:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {touchpart}

local bladecf = BladePart.CFrame
local rayorigin = bladecf.Position - bladecf.LookVector*2
local raydirection = bladecf.LookVector*10
local rayresult = workspace:Raycast(rayorigin, raydirection, params)

local hitposition
if rayresult then
    hitposition = rayresult.Position
else
    hitposition = bladecf.Position
end

-- create particles at hitposition

This casts the ray from a position 2 studs behind the blade in a direction 10 studs forwards and looks only for the part that was touched. A hit position is returned if the raycast result exists; otherwise, it uses the blade’s position as a fallback. Granted this assumes that the bladepart is orientated with its lookvector facing forwards. If not, you could try getting the direction by tracking the blade’s position through multiple frames and comparing the 2 different positions.

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