Guns bullet move to its mouse Issue

Hi!

I need help because this is taking me to long to even figure it out.

When The players bullet fires it works fine and anything else, but when I
shoot through a wall or a forcefield it just goes somewhere else

Here is the video

This is on the module for the gun

local direction = (Mouse.Hit.p - Face.Face.WorldPosition).unit

Bullet.CFrame = CFrame.new(Bullet.Position, Bullet.Position + direction)

This is the guns local script

local mouse = Player:GetMouse()

Tool.Activated:Connect(function()
	
	if not GunDB then
		
		GunDB = true

		GameModule.FireWeapon(Tool.Handle, false, Dirty, Character, mouse)

		task.wait(Tool.CoolDown.Value)
		GunDB = false
		
	end
	
end)
1 Like

Hi there!
You issue is due to your mouse cursor technically hitting the CanCollide false part in front of the target player.
To prevent this, simply set the property “CanQuery” to false for any part that you don’t want the players aim to be obstructed by.

That kind of worked but I am gonna need CanQuery for the AI (target) to be able to
detect you easier using raycast.

Then you may want to use Mouse.TargetFilter (docs)
TargetFilter is a table of Instances which will be ignored when calculating Mouse.Hit and Mouse.Target.

Simply create a table containing all the parts you wish to ignore, and set Mouse.TargetFilter to be that table.

mouse.Hit will be nil if you have no target, try raycasting or using the orientation of workspace.CurrentCamera (given its center-screen regardless) as a directional vector

edit: to clarify, when I say “you have no target”, that means like the sky, literally nothing. you could also encase your map in invisible walls to remove the issue entirely as well.

also you can just use

Bullet.CFrame = CFrame.new(Bullet.Position, mouse.Hit.p)

as that CFrame constructor type will just face the bullet toward the other position without the need of adding a position and a directional vector

1 Like

and thats just one issue, with the forcefield thing, I would as the other guy said TargetFilter works but you should probably be just raycasting and using RaycastParams because its a bit easier and more contemporary

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