Hey I’m currently working on a grenade, and I’m implementing rays to make it so if a player is behind a wall then the grenade won’t damage them. The problem I’m having is creating a ray between the players character HumanoidRootPart and the explosion. I can use Ray.new to create a ray however when i do the following it would only ever print “baseplate was hit”, so i added a ignore list, now it prints nothing. What it should be printing is my characters HumanoidRootPos or Torso since thats the direction it should be set to, this is a snippet of my current code
for i,v in ipairs(Players:GetChildren()) do
distance = (v.Character.HumanoidRootPart.Position - newclone.Position).Magnitude
print(distance)
if distance <= 100 then
local damage = 100 - distance
local POSITION = newclone.Position
local ray = Ray.new(POSITION,(newclone.Position - v.Character.HumanoidRootPart.Position).Unit * distance)
local hitPart, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, ignorelist)
if hitPart then
print("Hit Part".. hitPart:GetFullName())
else
print("Did not hit part")
end
end
end
Am I doing something wrong? Should I not be using Ray.New? and Instead using the ray functions Vector3 Ray:ClosestPoint ( Vector3 point ) For clarification my ignore list is a table so it is definitely ignoring and following the ray parameters. However it’s direction isn’t targeted at my HRP
i’m not sure if your maths is following the actual ray but yeah theirs a obvious pattern we can see in these two images, do you think you see a reason for why this is occurring?
local magnitude = (POSITION - (POSITION + ray.Direction) * distance).Magnitude
I think this will create a part that looks identical to what your ray looks like before raycast.
I suggest just messing a bit to debug, for example, turn your unit to negative to see what happens and it could give you clues to debug. Honestly, I think your code should work and I believe the maths I gave you is tested and correct.
I think you forgot to multiply the distance to the end result then get magnitude which is why you got a tiny ray.
Making the unit negative would reverse the unit which I recommended as an example that should be the same as what @imalex4 replied with for debug but it is better just to reverse the positions.
EDIT: Turns out you have to set a CFrame lookAt, replace newclone.CFrame with CFrame.new(POSITION, hitPosition instead. This will correct the debug code I gave you.