Raycast returns nil?

I am trying to create a ray between the player and the tip of their gun (which is castData.Origin)

Even if I reverse the lookDirection for the ray, I still get nil returned.

print(Character.HumanoidRootPart.Position)
print(castData.Origin)
print(Character.HumanoidRootPart.Position - castData.Origin)
local WallRaycast = workspace:Raycast(
	Character.HumanoidRootPart.Position,
	Character.HumanoidRootPart.Position - castData.Origin,
	CastParams
)

14:17:03.960 82.266883850098, -14.519752502441, 240.41772460938 - Server - RaycastManager:143
14:17:03.961 85.064659118652, -13.684807777405, 243.48907470703 - Server - RaycastManager:144
14:17:03.961 -2.7977752685547, -0.83494472503662, -3.0713500976563 - Server - RaycastManager:145

Even if there is 100% a part between my character and the castData.Origin, I get nil

1 Like

What does your setup for CastParams look like?
Also, I believe I ran into a similar issue a while back where if the ray (direction vector) provided to a raycast doesn’t hit the outside faces (aka the ray is completely enclosed within the part) then it will not detect it.

local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.IgnoreWater = true

--------------------------------

CastParams.FilterDescendantsInstances = {Character}

if GameSettings:GetAttribute("Teams") then -- Team based gamemode
	CastParams.CollisionGroup = player.Team.Name == "Blue" and "BluePaint" or "RedPaint"
end

print(Character.HumanoidRootPart.Position)
print(castData.Origin)
print(Character.HumanoidRootPart.Position - castData.Origin)
local WallRaycast = workspace:Raycast(
	Character.HumanoidRootPart.Position,
	Character.HumanoidRootPart.Position - castData.Origin,
	CastParams
)

So you want a ray starting at the HumanoidRootPart’s position right? I think you may want this instead:

local WallRaycast = workspace:Raycast(
	Character.HumanoidRootPart.Position,
	 castData.Origin - Character.HumanoidRootPart.Position,
	CastParams
)

Although you did already say you tried reversing it, is this what you meant by reversing?

Ye, I’;ve tried that and got nil :confused:

Hmm… and you’re absolutely sure that a part exists (that isn’t a child of the Character) between these points? You could try a bit of manual debugging by creating a part at the origin of the raycast and another at the end to visually see where the ray is being cast. Something like:

origin = Instance.new("Part", workspace)
origin.Anchored = true
origin.Size = Vector3.new(0.2,0.2,0.2)
origin.CFrame = CFrame.new(Character.HumanoidRootPart.Position)

dest = Instance.new("Part", workspace)
dest.Anchored = true
dest.Size = Vector3.new(0.2,0.2,0.2)
dest.CFrame = CFrame.new(Character.HumanoidRootPart.Position + (castData.Origin - Character.HumanoidRootPart.Position))
-- or whatever you're providing to the 2nd argument of the raycast call ^

With what you’ve provided, this is what I get

Red parts being the 2 parts. So it seems dest is being set at origin of the bullet :confused:

This also shows the raycast that’s being done on the bullet
image

And while I could use the Direction of the ray, I only want to check between the character and their gun for walls

-- Set up cast behavior
	local CastBehavior = FastCast.newBehavior()
	CastBehavior.RaycastParams = CastParams
	CastBehavior.Acceleration = Vector3.new(0, -GRAVITY, 0)
	
	local Origin = castData.Origin--firePoint
	local Direction = castData.Direction--(mousePosition - Origin).Unit
	
	local ActiveCast = Caster:Fire(Origin, Direction, SPEED, CastBehavior) -- Fire shot

EDIT Upon further testing, I think what I’ve got should work. I do this same ray on the client, and if there gun is inside a part, then I fire from their face. I was just trying to add an extra level of protection on the server in case anyone tries to bypass that