Fire a raycast, then tell player where raycast lands

  1. What do you want to achieve?
    how would i make a tool, that fires a raycast and then prints the position where the raycast landed

  2. What solutions have you tried so far?
    i’m just a novice :sob: and i just want to try and understand how this works,

2 Likes

Well, you could get the mouse delta and then with the camera rays direction * 1000 and raycast the position, I will make an example of what I mean. Get more info here: about raycasting

local UIS = game:GetService("UserInputService")
local Tool --Path to the tool
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local RayParams = RayCastParams.new()

function Raycast()
   local mousePos = UIS:GetMouseLocation()

   local cam_ray = camera:ViewportPointToRay(mousPos.X, mousePos.Y)

   local main_ray = workspace:Raycast(cam_ray.Origin, cam_ray.Direction*1000, RayParams) 

   if main_ray ~= nil then
      --do what you like
   end
end

Hope this helps, @Nub36784 !

1 Like

Just took a peak into the documentation, and building on what @ZakiAndrea said, the raycast result gives quite a bit of info on what happened with the ray. I’m aware that they already noted the documentation, but I thought I would give a bit more info on the result side.

--From the Roblox documentation
local rayOrigin = Vector3.zero --change to your origin
local rayDirection = Vector3.new(0, -100, 0) --change to your direction

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

if raycastResult then
    --this might be what you're looking for
	print("Instance:", raycastResult.Instance)
	print("Position:", raycastResult.Position)
	print("Distance:", raycastResult.Distance)
	print("Material:", raycastResult.Material)
	print("Normal:", raycastResult.Normal)
else
	warn("No raycast result!")
end
1 Like

Yeah, this is the after part of what I’ve said before, but with more info on the rayresult properties, @12345koip helped you on this side, lol.
I have cool video from @GnomeCode youtube channel. It’s on the tds game series, I recommend checking that out, it explains very well the mouse raycasting!

That’s all for me here :wink:

I took Zaki code and made it fire a raycast using the mouse position and then printing information about where it landed.

local UIS = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")

function Raycast()
	local mousePos = UIS:GetMouseLocation()

	local cam_ray = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local raycastResult = workspace:Raycast(cam_ray.Origin, cam_ray.Direction*1000) 

	if raycastResult ~= nil then
		print("Instance:", raycastResult.Instance)
		print("Position:", raycastResult.Position)
		print("Distance:", raycastResult.Distance)
		print("Material:", raycastResult.Material)
		print("Normal:", raycastResult.Normal)
	end
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		Raycast()
	end
end)

thank you so so much, tbh i thought the script would be like, very very complicated, is there any way to “simplify” the print output?

Sure.

local UIS = game:GetService("UserInputService")
local camera = workspace.CurrentCamera

function Raycast()
	local mousePos = UIS:GetMouseLocation()

	local cam_ray = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local raycastResult = workspace:Raycast(cam_ray.Origin, cam_ray.Direction*1000) 

	if raycastResult ~= nil then
		print("Position:", raycastResult.Position)
	end
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		Raycast()
	end
end)

First you must create a Tool, add a part and name it Handle, now add a localscript and use this simple code.

local function onClick()
	local tool = script.Parent
	local toolHandle = tool. Handle
	local ray = Ray.new(toolHandle.Position, toolHandle.CFrame.LookVector * 300)
	local hitPart, hitPosition = workspace:FindPartOnRay(ray)

	if hitPart then
		print("Hit at:", hitPosition)
	else
		print("No hit")
	end
end

script.Parent.Activated:Connect(onClick)

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