RayCasting and direction to a instance

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

Debug this by replicating a part that represents your ray, this should work after the hitPart conditional statement:

local part = Instance.new(“Part”)
part.Anchored, part.CanCollide = true, false
local magnitude = (POSITION - hitPosition).Magnitude
part.Size = Vector3.new(0.1, 0.1, magnitude)
part.CFrame = newclone.CFrame * CFrame.new(0, 0, -magnitude/2)
part.Parent = workspace

game.Debris:AddItem(part, 2.5)

This will visually show what your ray looks like

This is what I got,


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?

EDIT: i now just got this ??? image

You can also switch magnitude with the ray.

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.

You need to switch around the root part and POSITION. Use this instead.

local ray = Ray.new(POSITION,(v.Character.HumanoidRootPart.Position - POSITION).Unit * distance)

When getting a direction vector, remember: that - this.

Yeah its not a accurate representation. no worries though, thank you for helping

Thanks! Could you explain why this made the direction vector work though, what was wrong with the way I was doing it

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.

Your ray starts at POSITION, but the direction goes off into nowhere (or the baseplate).

Imagine that POSITION = 0, 0, 0 and Torso = 0, 1, 0 (1 stud above POSITION)

Doing POSITION - Torso = 0, -1, 0 which gives you the opposite direction you need.

Torso - POSITION = 0, 1, 0 which is the direction from POSITION to Torso.

remember that minus this

1 Like