Hey! I will be quick here. I am making a gun system and I came across an issue which I will be showing on a video later.
Here is a part of the code of the gun system:
function FireHandler:FireWeapon(player, weaponCategory, weaponName, gun)
local firingPart
for index, value in pairs(gun:GetDescendants()) do
if value.Name == "MuzzlePoint" and value:IsA("Attachment") then
firingPart = value
break
end
end
local projectileVelocity = self.weaponList[weaponCategory][weaponName]["MuzzleVelocity"]
local projectile = Instance.new("Part")
projectile.Size = Vector3.new(0.5, 0.1, 0.1)
projectile.Position = firingPart.Position
projectile.Parent = game.Workspace
projectile.BrickColor = BrickColor.new("Bright orange")
projectile.Material = Enum.Material.Neon
projectile.CFrame = CFrame.new(firingPart.WorldPosition, firingPart.WorldPosition + (firingPart.WorldCFrame.LookVector))
projectile.CanCollide = false
local BodyVelocity = Instance.new("BodyVelocity", projectile)
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = (firingPart.WorldCFrame.LookVector * projectileVelocity)
projectile:SetNetworkOwner(player)
game:GetService("Debris"):AddItem(projectile, 25)
projectile.Touched:Connect(function(hit)
if hit.Parent ~= gun:GetDescendants() or player.Character then
if hit.Parent:IsA("Model") and game.Players:GetPlayerFromCharacter(hit.Parent) then
local hitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
self.ImpactPlayerHandler:HandleImpact(hitPlayer, weaponCategory, weaponName, hit)
else
local penetrationDepth = self.PenetrationHandler:CalculatePenetrationDepth(weaponCategory, weaponName, hit)
if penetrationDepth > hit.Size.Z then
print("The projectile penetrated the part: "..hit.Name)
else
print("The projectile did not penetrate the part: "..hit.Name)
end
end
end
--projectile:Destroy()
end)
end
Video:
As you can see:
The bullet doesn’t even start at the muzzle point (the attachment or “firingPart”).
The bullet won’t go to the correct direction, but the default one of the attachment.
The bullet has a wrong rotation.
Don’t mind the error I got. It is non-related to this problem.
function FireHandler:FireWeapon(player, weaponCategory, weaponName, gun)
local firingPart
for index, value in pairs(gun:GetDescendants()) do
if value.Name == "MuzzlePoint" and value:IsA("Attachment") then
firingPart = value
break
end
end
local projectileVelocity = self.weaponList[weaponCategory][weaponName]["MuzzleVelocity"]
local projectile = Instance.new("Part")
projectile.Size = Vector3.new(0.5, 0.1, 0.1)
projectile.Position = firingPart.WorldPosition
projectile.Parent = game.Workspace
projectile.BrickColor = BrickColor.new("Bright orange")
projectile.Material = Enum.Material.Neon
projectile.CFrame = CFrame.new(firingPart.WorldPosition, firingPart.WorldPosition + (firingPart.WorldCFrame.LookVector))
projectile.CanCollide = false
local BodyVelocity = Instance.new("BodyVelocity", projectile)
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = (firingPart.WorldCFrame.LookVector * projectileVelocity)
projectile:SetNetworkOwner(player)
game:GetService("Debris"):AddItem(projectile, 25)
projectile.Touched:Connect(function(hit)
if hit.Parent ~= gun:GetDescendants() or player.Character then
if hit.Parent:IsA("Model") and game.Players:GetPlayerFromCharacter(hit.Parent) then
local hitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
self.ImpactPlayerHandler:HandleImpact(hitPlayer, weaponCategory, weaponName, hit)
else
local penetrationDepth = self.PenetrationHandler:CalculatePenetrationDepth(weaponCategory, weaponName, hit)
if penetrationDepth > hit.Size.Z then
print("The projectile penetrated the part: "..hit.Name)
else
print("The projectile did not penetrate the part: "..hit.Name)
end
end
end
--projectile:Destroy()
end)
end
Ah yeah, that might need some messing around with CFrame’s Angles. Try adding something like * CFrame.Angles(math.rad(90),0,0) to your CFrame. (mess around with xyz and values till you find out the perfect one!)
If you check the bullet rotation in the video when I fire the “gun”, it is incorrect. I think it has to do with the size of the part. Let me check.
Edit: Yes, it was because of the size. It is should be 0.1,0.1,0.5 instead of 0.5, 0.1, 0.1