How to raycast from an object to the center of the screen

Hello Developers,

I am having an issue with trying to create a ray that shoots out from the camera and hit where the user is looking at. The ray should start at the gun’s “Flare” part, which is a part inside of my gun model, related to the barrel of a gun. Any help will be appreciated!

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)
local HitPoint = raycastResult.Position -- vector3 that player is looking at

this kinda stuff has been answered before, try searching on google before making posts. I gave a barely modified script from the link of where it has been answered before. It gives a point from the client that should be then fired to a server script using a remote event that uses it as an endpoint/direction that a ray should be fired. I’m assuming you know what your doing, but I’d be happy to help you make the rest of it if you aren’t sure how to do this.

The Camera object has a function that creates a ray from a screen/viewport point. You can pass any point on screen to it:

I tried using that method, it didn’t work for me, for some reason.

1 Like

Also tried this, I read through the whole documentation, however, maybe its just my poor reading skills, but I couldn’t seem to get it working with any of the methods in the camera documentation.

where exactly is it not working? Are you getting the position the camera is pointed, but messing up making the gun fire in that direction? Is even the first part which is just getting the position from the camera? If thats the case, make sure to use a local script, but I don’t really know where you are and what part isn’t working.

This is the code I am using.

local Camera = workspace.CurrentCamera
local unitRay = Camera:ScreenPointToRay(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)
local ray = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, rayCastParams)

I don’t understand why it doesn’t work :confused:

What type of script are you using? Where did you put it? Could you send your code?

and this is running in a local script, like every time you click or something so you can test it on demand? Id chuck a few print’s here and there to see what’s happening, some print at the start to see if its running at all, and a print(ray) at the end to see if it did anything.

I am using it in a server script. Its in the gun I made. If you want ill give a screenshot of where it is located.

I have a local script checking for when the tool is activated:

local Gun = script.Parent
local Dispenser = script.Parent:WaitForChild("Flare")

local fireGun = script.Parent:WaitForChild("FireGun")
local OnCooldown = false

Gun.Activated:Connect(function()

	if OnCooldown then return end
	fireGun:FireServer(Dispenser.CFrame)
	OnCooldown = true
	task.wait(0.1)
	OnCooldown = false

end)

Try sending the raycast in the local script and then firing the remote event to damage the player

so, the server doesn’t have a camera, ill let @RobloxKing516 take it from here because it seems like he knows what he’s talking about

ill try it, I tried it before it didn’t work, but ill try again, how should I approach it, like when the gun is activated and it sends the message to the server, should I send the raycast result over to the server too?

I am a new developer, so sorry if I don’t know too much…

Yeah, ive made a 3d gun system which i believe is pretty similar to this, so first off just make it send the raycast through the local script ( as you can only then use the camera ) and then send what the raycast hit to the server for it to do damage

Okay, ill try it right now and tell you the results.

no worries, if you need help with the scripts or let me know, you can also use chat gpt if you are really struggling

I thought chatgpt is prohibited when coding?