Projectiles start with incorrect position and rotation

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.

Any help is appreciated!

1 Like

For issues with position, use firingpoint’s WorldPosition, not Position. Position is the offset from it’s center.

2 Likes

Like that? If so it still looks the same.

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
1 Like

I do notice you have both .Position and .CFrame in there. Hang on, I’m still trying to figure out what could be wrong in there.

1 Like

I had had the same problem once before and I posted it in devForum. I checked it but still couldn’t figure out the problem.

Bullets starting from a specific position instead of the muzzle - Help and Feedback / Scripting Support - Developer Forum | Roblox

1 Like

Hey! I found the issue. It was because I was passing the gun parameter as a toolmodel stored in a folder instead of the equipped one.

2 Likes

I just realized the rotation of the projectile is incorrect. @Qariter

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!)

2 Likes


This is what I mean by the way.

Are you talking about the bullets going backwards, or the model being backwards?

1 Like

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

1 Like

Oh, problem solved then? Sorry I couldn’t help a bit earlier!

1 Like

It’s fine! I really appreciate your help! I really want to create a GOOD gun system which would be made for public use in the future.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.