I had a problem with weapons in first person. When I play on PC, the raycast bullet shoots and hits through the center of the screen and not on the phone, it hits the position that I click on the screen, i.e. the fire button.
Here is script to fire remove event:
--localscript
local mouse=plr:GetMouse()
local point=weapon.ShootPoint
ReplicatedStorage.Remotes:Fire:FireServer(mouse.Hit.Position,point.Position)
And here is on remote event:
--script
game.ReplicatedStorage.Remotes.Fire.OnServerEvent:Connect(function(plr,pos,shootPos)
local rayCast=Ray.new(shootPos,(pos-shootPos).unit*3000)
local part,Position,normal=workspace:FindPartOnRayWithIgnoreList(rayCast,{workspace.Bullets,workspace.Camera},false,true)
if part then
local hum=part.Parent:FindFirstChild("Humanoid")
if not hum then
local hole=game.ReplicatedStorage.Mix.hole:Clone()
hole.Parent=part.Parent
hole.FadeScript.Disabled=false
hole.Position=pos
hole.CFrame=CFrame.new(hole.Position,hole.Position+normal
end
end
end)
I have seen that it is sort of done through
Camera.Focus.Position
or
Camera.CoordinateFrame.Position
But I don’t quite understand how. camera.CFrame.LookVector / Position,camera.Focus.LookVector / Position don’t work for me
local spread=CFrame.Angles(math.rad(math.random(-spr, spr/20)),0,math.rad(math.random(-spr, spr/20)))
game.ReplicatedStorage.Fire:FireServer(cam.CFrame.Position,cam.Focus.Position,shot.Position,spread) -- screen center, center direction, muzzle point of gun (for laser and rocket launcher)
server side:
game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(plr,pos,dir,shot,spread)
if not spread then spread=1 end
local rayCast=Ray.new(pos,(CFrame.new(pos,dir)*spread).LookVector.unit*1000)
local part,position,normal=workspace:FindPartOnRayWithIgnoreList(rayCast,{workspace.IgnoreFolder,plr.Character},false,true)
if part then
local hum=part.Parent:FindFirstChild("Humanoid")
if not hum then
local laser=Instance.new("Part", workspace.IgnoreFolder)
laser.CanCollide=false
laser.Anchored=true
laser.Parent=workspace.Ignore
laser.Color=Color3.new(1,1,0)
laser.Transparency=.6
laser.Material="Neon"
laser.Name="Laser"
local dist=(position-shot).Magnitude
laser.CFrame=CFrame.new(shot,position)*CFrame.new(0,0,-dist/2)
laser.Size=Vector3.new(.1,.1,dist)
game.Debris:AddItem(laser,.1)
end
end
end)