Help with splash code

for some reason my splash is way off from were it hit, any help?

local part = script.Parent
local raycastDistance = 100 

part.Touched:Connect(function(hit)
	if not hit.Parent:FindFirstChild("Humanoid") then
		local raycastResult = workspace:Raycast(part.Position, (hit.Position - part.Position).Unit * raycastDistance)
		if raycastResult then
			local hitPart = raycastResult.Instance
			local hitCFrame = hitPart.CFrame
			local randomnum = math.random(1, 3)
			local splashpart = game.ReplicatedStorage.Splash:FindFirstChild("splash"..randomnum):Clone()
			splashpart.Parent = workspace

			local hitLookVector = hitCFrame.LookVector
			local hitUpVector = Vector3.new(0, 1, 0) -- Set the up vector to point upwards
			local hitRightVector = hitLookVector:Cross(hitUpVector)

			splashpart.CFrame = CFrame.new(raycastResult.Position, raycastResult.Position + raycastResult.Normal)
			splashpart.CFrame = splashpart.CFrame * CFrame.Angles(0, math.rad(90), 0)

			splashpart.Color = script.Parent.Color
			script.Parent:Destroy()
		end
	end
end)

5 Likes

it could possibly be something to do with adding the normal to the raycast result position

3 Likes

I don’t see how this would make it not acurate as I researched quite a few devforum posts on trying to make this and I saw them all add the normal

2 Likes

what does adding the normal do exactly?

2 Likes

Im not exactly sure but I believe it is perpendicular to the surface

remove it and see what happens. from there, you may be able to find a solution

1 Like

I tried that but it did not work

what changed when you got rid of that bit?

1 Like

It broke and put it in the normal CFrame of the part that was not moved

you made sure you did:

splashpart.CFrame = CFrame.new(raycastResult.Position)

?

1 Like

No this script is like this tio get the position and rotation not just the position

1 Like

the normal sets the rotation??

1 Like

I’m not sure what the problem is, but you could try starting the raycast from above the part by a small offset like so:

local raycastResult = workspace:Raycast(part.Position + Vector3.new(0,1,0), (hit.Position - part.Position).Unit * raycastDistance)

Or you could try to, similar to what @Chiseled_Cheese mentioned, set the CFrame of the splash differently:

splashpart.CFrame = CFrame.new(raycastResult.Position) * CFrame.Angles(0, math.rad(90), 0)

I’m not familiar with the normal vectors, so I’m not sure where the RaycastResult.Normal comes into play…

1 Like

fixed it myself
(ffdsfdsfdfdsfsds)

1 Like

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