so im trying to make a gun that shoots in direction of mouse in normal but when its used by a vr player its shoots straight, just the problem is that im terribly bad at math and i dont know how to make it shoot straight out of the barrel this is the code i have so far
also i switched the vr and pc check bc its easier to test in pc
local script:
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local vrservice = game:GetService('VRService')
script.Parent.Activated:Connect(function()
if vrservice.VREnabled == false then
script.Parent.Events.ShootRE:FireServer(mouse.Hit)
else
script.Parent.Events.ShootRE:FireServer("vr")
end
end)
server script:
script.Parent.Events.ShootRE.OnServerEvent:Connect(function(plr,mousehit)
script.Parent.Handle.FireSound:Play()
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {plr.Character}
local result
if mousehit == "vr" then
result = workspace:Raycast(script.Parent.Handle.Attachment.WorldPosition,(mousehit.Position - script.Parent.Handle.Attachment.WorldPosition).Unit * 1000,params)
else
result = workspace:Raycast(script.Parent.Handle.Attachment.WorldCFrame.Position,(script.Parent.Handle.Attachment.WorldCFrame.LookVector).Unit * 100,params)
end
print(result)
if result and result.Instance.Parent == workspace.Lui then
local blood = game.ReplicatedStorage.Blood:Clone()
blood.Parent = result.Instance
blood.Position = result.Position
local weld = Instance.new("WeldConstraint")
weld .Parent = blood
weld .Part0 = blood
weld.Part1 = result.Instance
blood.Blood3.Enabled = true
local bodyvelocity = Instance.new("BodyVelocity")
bodyvelocity.MaxForce = Vector3.new(1e9,1e9,1e9)
bodyvelocity.Parent = result.Instance
bodyvelocity.Velocity = CFrame.new(result.Position,result.Position+result.Normal).LookVector * -10
game:GetService("Debris"):AddItem(bodyvelocity,0.1)
game:GetService("Debris"):AddItem(blood,0.5)
local hole = game.ReplicatedStorage.Hole:Clone()
hole.Parent = result.Instance
hole.CFrame = CFrame.new(result.Position,result.Position + result.Normal)
local weld2 = Instance.new("WeldConstraint")
weld2.Parent = hole
weld2.Part0 = hole
weld2.Part1 = result.Instance
workspace.Lui.Humanoid:TakeDamage(100)
end
end)