I want to make a gun with CFrame and Raycasts
But Raycast.Position works incorrectly!
yellow line is beam from gun to bullet
blue line is Raycast direction
red is showing the difference of raycast direction and result (From bullet to Raycast.Position)
I tried to search solution but don`t found it
Here`s my code
local RunService = game:GetService("RunService")
local DebrisService = game:GetService("Debris")
local tool = script.Parent
local Model = tool.Model
local RemoteEvent = tool.RemoteEvent
local ToolProperties = {MaxAmmo = 6,CurrentAmmo = 6,ReloadTime = 2,CooldownTime = 0.5,IsReloading = false}
local BulletProperties = {BulletSpeed = 1,BulletDamage = 20}
local function HitTest(BulletPos:Vector3,RaycastHitPos:Vector3,GunPos:Vector3,RaycastDirection:Vector3)
local attachment00 = Instance.new("Attachment")
attachment00.WorldCFrame = CFrame.new(BulletPos)
attachment00.Parent = workspace.Terrain
local attachment10 = Instance.new("Attachment")
attachment10.WorldCFrame = CFrame.new(RaycastHitPos)
attachment10.Parent = workspace.Terrain
local Beam0 = Instance.new("Beam")
Beam0.Attachment0 = attachment00
Beam0.Attachment1 = attachment10
Beam0.Parent = workspace
local attachment01 = Instance.new("Attachment")
attachment01.WorldCFrame = CFrame.new(GunPos)
attachment01.Parent = workspace.Terrain
local attachment11 = Instance.new("Attachment")
attachment11.WorldCFrame = CFrame.new(BulletPos)
attachment11.Parent = workspace.Terrain
local Beam1 = Instance.new("Beam")
Beam1.Attachment0 = attachment01
Beam1.Attachment1 = attachment11
Beam1.Parent = workspace
local attachment02 = Instance.new("Attachment")
attachment02.WorldCFrame = CFrame.new(GunPos)
attachment02.Parent = workspace.Terrain
local attachment12 = Instance.new("Attachment")
attachment12.WorldCFrame = CFrame.new(RaycastDirection)
attachment12.Parent = workspace.Terrain
local Beam2 = Instance.new("Beam")
Beam2.Attachment0 = attachment02
Beam2.Attachment1 = attachment12
Beam2.Parent = workspace
Beam0.Color = ColorSequence.new(Color3.new(1, 0, 0))
Beam1.Color = ColorSequence.new(Color3.new(1, 1, 0))
Beam2.Color = ColorSequence.new(Color3.new(0, 0, 1))
end
local function Shoot(Mouse,ModelCframe:CFrame,Player:Player)
local Bullet = Instance.new("Part")
Bullet.CFrame = ModelCframe
Bullet.Size = Vector3.new(0.25,0.25,BulletProperties.BulletSpeed)
Bullet.Color = BrickColor.new("Neon orange").Color
Bullet.CanCollide = false
Bullet.Anchored = true
Bullet.Parent = workspace
DebrisService:AddItem(Bullet,120)
while true do
task.wait()
Bullet.Position += ModelCframe.LookVector*BulletProperties.BulletSpeed
local Raycast = workspace:Raycast(Bullet.Position,Bullet.Position+Bullet.CFrame.LookVector*BulletProperties.BulletSpeed*10)
if Raycast and Raycast.Instance ~= nil then
if Raycast.Instance.Parent ~= workspace:FindFirstChild(Player.Name) and Raycast.Instance.Parent.Parent ~= workspace:FindFirstChild(Player.Name) then
HitTest(Bullet.Position,Raycast.Position,ModelCframe.Position,Bullet.Position+Bullet.CFrame.LookVector*BulletProperties.BulletSpeed*10)
if Raycast.Instance.Anchored == true then
local Hit = Instance.new("Part")
Hit.Position = Raycast.Position
Hit.Size = Vector3.new(0.25,0.25,0.25)
Hit.Color = BrickColor.new("Black").Color
Hit.CanCollide = false
Hit.Anchored = true
Hit.Parent = workspace
DebrisService:AddItem(Hit,60)
end
Bullet:Destroy()
break
end
end
end
end
RemoteEvent.OnServerEvent:Connect(function(Player:Player,Mouse,ModelCframe)
if ToolProperties.IsReloading == false then
if ToolProperties.CurrentAmmo > 0 then
ToolProperties.IsReloading = true
Model["spy revolver fire sound"]:Play()
task.spawn(Shoot,Mouse,ModelCframe,Player)
ToolProperties.CurrentAmmo -= 1
wait(ToolProperties.CooldownTime)
ToolProperties.IsReloading = false
else
ToolProperties.IsReloading = true
Model["Revolver Reload"]:Play()
task.wait(ToolProperties.ReloadTime)
ToolProperties.CurrentAmmo = ToolProperties.MaxAmmo
ToolProperties.IsReloading = false
end
end
end)