Making a ray from the middle of the screen with the camera of a player

Hello!

I want to make a grapple gun system but I have a problem with MOBILE.
So mouse.Target on mobile is not good. On mobile you can click anywhere on the screen while on a computer (Your mouse is locked) you can only press the middle. I want it to work on my mobile by pressing a button on the screen to fire your grapple gun and retract it by pressing the button again. But I need an alternative to mouse.Target to still know what part the mobile player is clicking on with their fake mouse else I don’t know which part to grapple to. I have tried searching for systems but I don’t have a clue on how to actually make it work. It just keeps on failing. Or the ray doesn’t hit any parts. And I don’t want it to face with the head orientation, I want it to rotate the ray with the camera rotation of the player. It’s in first person by the way.

3 Likes

Hey there! Have you tried using CFrame of workspace.CurrentCamera for the raycast?

How would you go about making a ray with CFrame of the workspace.CurrentCamera?

I want the length of the ray to be like 1000 studs long.

You can do something like this (client side):

local camera = workspace.CurrentCamera
local cameraCF = camera.CFrame

local length = 1000
local params = RaycastParams.new()

local raycastResult = workspace:Raycast(cameraCF.Position, cameraCF.LookVector * length, params)

you can read more about raycasting over here: WorldRoot | Roblox Creator Documentation

8 Likes

Do you know how to get the position of where the ray hits. (Not the position of the part it hits) like mouse.Hit.Position but with rays.

You are looking for raycastResult.Position

2 Likes

Doesn’t that get the position of the part it hits instead of the position that the ray interacts with the part.

raycaseResult.Position is “The world space point at which the intersection occurred, usually a point directly on the surface of the instance.”, you can read more here: RaycastResult | Roblox Creator Documentation

2 Likes

So that’s the different between Ray.new() and workspace:Raycast()

Yes it is. workspace:Raycast() is newer and better while Ray.new() is old and deprecated.

2 Likes

it seems that the direction of the camera is a little above the center of the camera, I think this is due to the top bar. any way to fix this?

2 Likes