Hello
How can I find the coordinates of the point, which lies on the surface directly below my cursor position (it could be the terrain or it could be some Part)?
I want to make “click to teleport” function, but I would like to show some “sign” there, so the user can know in advance the exact position in which he will be teleported.
I made something simple, however though the part does react to itself. I can’t seem to figure out a solution, but here is the base of it. Import this into StartPlayerScripts with a LocalScript
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local TweenService = game:GetService("TweenService")
local cursorPart = Instance.new("Part")
cursorPart.Anchored = true
cursorPart.CanCollide = false
cursorPart.Size = Vector3.new(1, 1, 1)
cursorPart.Parent = game.Workspace
-- Update the cursor part's position to match the cursor position
while true do
local cursorPos = mouse.Hit.p
local partCF = CFrame.new(cursorPos)
-- Create a tween to smoothly move the cursor part to the new position
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear)
local tween = TweenService:Create(cursorPart, tweenInfo, {CFrame = partCF})
tween:Play()
wait(0.01)
end
This is a function I made for something similar. function getRayCastHit(originPart, originPoint, direction, maxDistance)
if maxDistance == nil then
maxDistance = math.huge
end
local part, point, normal, material = workspace:FindPartOnRayWithIgnoreList(Ray.new(originPart.CFrame:PointToWorldSpace(originPoint), direction.Unit * maxDistance), {originPart})
return part, point, normal
end
It takes an origin part (the Part you’re casting the ray from) and an origin point (the point in local space from which you want to cast from), then a direction vector (the direction vector of the ray), and an optional maximum distance value.
Then it returns a tuple of the part, point, and normal.