Bullet for gun not working

Hello! I am trying to make a gun for my game, and for the most part its going well! However, when I try to add bullets for it, it seems to not work. The bullets don’t go in the right direction, and aren’t rotated correctly. And the raycasting doesn’t have the player take damage, even though it does without the bullet.

Video:

Bullet code:

	local bullet = RS:WaitForChild("Bullet")
	
	local new_bullet = bullet:Clone()
	new_bullet.Anchored = false
	new_bullet.CanCollide = false
	new_bullet.Position = handle["Model12&@321"].FIRE_AREA.Position
	new_bullet.Parent = game.Workspace
	DEBRIS:AddItem(new_bullet, 4)

	new_bullet.Velocity = new_bullet.CFrame.LookVector * speed
	
	local character = player.Character
	
	--Raycasting
	local ray_params = RaycastParams.new()
	ray_params.FilterType = Enum.RaycastFilterType.Blacklist
	ray_params.FilterDescendantsInstances = {character}
	
	local raycast_result = workspace:Raycast(handle["Model12&@321"].FIRE_AREA.Position, (mouse_pos - handle["Model12&@321"].FIRE_AREA.Position) * 300, ray_params)
	
	
	if raycast_result then
		local instance = raycast_result.Instance
		local model = instance:FindFirstAncestorOfClass("Model")
		
		if model then
			if model:FindFirstChild("Humanoid") then
				if instance.Name == "Head" then
					headshot_sound:Play()
					model:FindFirstChild("Humanoid"):TakeDamage(25)
				else
					model:FindFirstChild("Humanoid"):TakeDamage(15)
				end
			end
		end
	end

Any help will be appreciated :cowboy_hat_face:

First, RaycastFilterType.Blacklist has been deprecated and you should use Enum.RaycastFilterType.Exclude instead. You can read a bit more about it here.

Second, I believe the reasoning as to why the direction and orientation of the bullets are incorrect is because you are setting the Raycast from the .Position rather than .CFrame. CFrame should accurately be able to get the position and orientation of the projectile.

1 Like

I have adjusted the code to where it uses CFrame, but now the bullet just trails a million feet into the air and then falls down.

After watching a quick tutorial on gun physics and how to add projectiles into your weapons, I can safely say I have fixed this.

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