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.
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.
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)
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 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)
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?
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