Object being welded to Player on .Touched is floating above

I’m making a cactus where when the Player touches the cactus a thorn will be created and welded to the player in the collision spot, I’m using Raycasting for this.
But the thorn object is placed way above the place where the player and cactus collided
Any ideas?

Here is my code:

			local rayOrigin = cactus.Position
			local rayDestination = character.HumanoidRootPart.Position
			local rayDirection = (rayDestination - rayOrigin).Unit * 500
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

			if raycastResult ~= nil then
				local distance = (rayOrigin - raycastResult.Position).Magnitude
			
				local rayOrigin = cactus.Position
				local rayDestination = character.HumanoidRootPart.Position
				local rayDirection = (rayDestination - rayOrigin).Unit * 500
				local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

				if raycastResult ~= nil then
					local thorn = Instance.new("Part")
					thorn.Size = Vector3.new(0.1, 1, 0.1)
					thorn.Shape = Enum.PartType.Wedge
					thorn.CanCollide = false
					thorn.Color = Color3.fromRGB(211, 194, 132)
					thorn.Anchored = true
					thorn.Parent = workspace.temp

					local surfacePosition = hit.CFrame:PointToWorldSpace(Vector3.new(0, hit.Size.Y/2, 0))
					thorn.CFrame = CFrame.new(surfacePosition, surfacePosition + rayDirection)

					local weld = Instance.new("WeldConstraint")
					weld.Part0 = thorn
					weld.Part1 = hit
					weld.Parent = thorn

					thorn.Anchored = false
					game.Debris:AddItem(thorn, 120)
				end
			end

Where is the variable hit from?
What are the raycasts actually trying to do?
If you’d explain your code a little bit, it would help people to be able to deduce the problem easier.

2 Likes

Why are there two raycasts being casted? Perhaps whats happening is that the raycast is being cast through the body part, and hitting the part that’s behind the player?

1 Like

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