Working on a raycast blood system

The raycast work fine in the ground, however, when the raycast is sent to a wall, for example it does this:

here is the code

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, character)
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	
	local part = script.Part:Clone()
	
	local rayOrigin = humanoidRootPart.Position
	local rayDirection = rayOrigin - Vector3.new(100, 0, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	local instance = raycastResult.Instance
	if instance then
		part.Parent = workspace
		part.Orientation = instance.Orientation
		part.Position = raycastResult.Position
	end
end)
1 Like

Use raycastResult.Normal for blood orientation. And convert it to CFrame using CFrame.LookAt (Or function below)

local function CFLookAt2(eye:Vector3, target:Vector3, plane:CFrame)
	local VectorX, VectorY, VectorZ = plane and plane.XVector or Vector3.xAxis, plane and plane.YVector or Vector3.yAxis, plane and plane.ZVector or Vector3.zAxis
	local lookVector = target - eye
	if lookVector:Dot(lookVector) <= 1e-5 then
		return CFrame.new(eye)
	else 
		lookVector = lookVector.Unit
	end
	if math.abs(lookVector.Y) >= 0.9999 then
		return CFrame.fromMatrix(eye, VectorX, math.sign(lookVector.Y) * VectorZ)
	else
		local rightVec = lookVector:Cross(VectorY).Unit
		local upVec = rightVec:Cross(lookVector).Unit
		return CFrame.fromMatrix(eye, rightVec, upVec)
	end
end

(variable plane isn’t necessary to pass)

2 Likes

Oh ok, but how may I implement it?

2 Likes
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
    local Normal = raycastResult.Normal
    local Orientation = CFLookAt2(Vector3.zero, Normal) + raycastResult.Position
    Blood.CFrame = Orientation -- Blood is your blood part.
end
2 Likes


Still not working

1 Like

You may need to adjust base rotation. Try to multiply result by CFrame.Angles(X, Y, Z), where XYZ being:
0
math.pi/2
math.pi
-math.pi/2

local Orientation = CFLookAt2(Vector3.zero, Normal) * CFrame.Angles(X, Y, Z) + raycastResult.Position
2 Likes

What do you mean by “You may need to adjust base rotation. Try to multiply result by CFrame.Angles(X, Y, Z), where XYZ being”?

2 Likes

he means that the rotation is off. You can see in the images that it’s working but it’s 90 degrees off.

1 Like


It worked, but how can I make so that the blood does not stack on top of each other?

1 Like

when you make a blood part you can put it in a specific folder, and using raycastparams to filter the folder to ray you are casting it will ignore the blood already there and wont stack

2 Likes

It worked, thank you a lot too!

2 Likes

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