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