2D Shooter Shooting issues

Hello, I am having a issue in my 2D shooter
I want the bullet to shoot where my mouse is but it doesnt go in the correct direction

How the gun works in my gun is simple raycast get raycast position(And yes there is a wall behind the player so it doesnt give me an error)
Shoot the bullet towards this position
I tried everything to get the bullet to go to the position but nothing worked
I Spawned a part everytime the player shoots But for some reason the part spawns in correct Position while bullet go in wrong direction

I tried looking for posts with similar issues but i found nothing
Does anyone know the solution to this?

The bullets seem to initially go to the right position but seem to have a parabolic trajectory that makes them drop-off. Alter your projectile code so the bullets don’t “get affected” by gravity.

bullets getting affected by gravity is what i want + i tried using linear velocity which doesnt make it get affected by gravity but the same issue

i remember having inaccuracy issues with my 2d shooter too, and the problem was i was getting the mouse position before i updated the camera

i wish this was the issue but i get the camera the same time i get mouse position

1 Like

sharing some code would probably help.

have you added debug print statements in the key parts that tell you what the variable are , like your mouse X Y Z , and your target bullet end goal position?

Also if this is a 2D space, then you should have a fixed position on one of them because the bullet does not need to go further out past the player… perhaps the mouse is hitting the part which then offsets back to where the bullet thinks it needs to go. but like the X and Y would change and the Z would be static… or whichever one it is…

1 Like

Here is the script if ur wondering how this works

Local Script:

script.Parent.Activated:Connect(function()
	local UIS = game:GetService("UserInputService")
	local MouseLocation = UIS:GetMouseLocation()
	local Cam = game.Workspace.CurrentCamera
	local MouseVector = Cam:ViewportPointToRay(MouseLocation.X,MouseLocation.Y)
	script.Parent.RemoteEvent:FireServer(MouseVector)
end)

Server Script:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player,MouseVector)
	local Origin = MouseVector.Origin
	local Direction = MouseVector.Direction * 100000
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {Player.Character}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	local RayCast = workspace:Raycast(Origin,Direction,Params)
	--Ignore this, it has nothing related to the problem
	if RayCast.Position.X > Player.Character.HumanoidRootPart.CFrame.X then
		local Root = Player.Character.HumanoidRootPart
		local RootPos = Root.Position
		local LookAt = Vector3.new(RootPos.X + 50,RootPos.Y,RootPos.Z)
		Root.CFrame = CFrame.lookAt(Root.Position,LookAt)
	elseif RayCast.Position.X < Player.Character.HumanoidRootPart.CFrame.X then
		local Root = Player.Character.HumanoidRootPart
		local RootPos = Root.Position
		local LookAt = Vector3.new(RootPos.X + -50,RootPos.Y,RootPos.Z)
		Root.CFrame = CFrame.lookAt(Root.Position,LookAt)
	end
	
	local Visual = Instance.new("Part")
	Visual.Parent = workspace
	Visual.Color = Color3.new(255,0,0)
	Visual.Position = RayCast.Position
	Visual.Size = Vector3.new(0.5,0.5,0.5)
	Visual.CanCollide = false
	Visual.Anchored = true
	
	local Velocity = RayCast.Position.Unit * 60
	Velocity = Vector3.new(Velocity.X,Velocity.Y,0)
	local BulletStart = script.Parent.BulletStart.Position
	local Bullet = Instance.new("Part")
	Bullet.Size = Vector3.new(0.25, 1, 0.25)
	Bullet.Position = Vector3.new(BulletStart.X,BulletStart.Y,2)
	Bullet.Color = Color3.fromRGB(255, 255, 0)
	Bullet.Material =Enum.Material.Neon
	Bullet.AssemblyLinearVelocity = Velocity
	Bullet.Parent = workspace
	Bullet:SetNetworkOwner(Player)
end)

from what I can tell, your issue is that you’re doing this:

local Velocity = RayCast.Position.Unit * 60

which takes the world-origin direction of RayCast.Position,
not the direction from your gun to the hit point,
measure the difference between the hit position and your bullets start,
normalise that, then multiply by your speed;

local targetPos = RayCast.Position
local startPos = BulletStart.Position
local direction = (targetPos - startPos).Unit
local Velocity = Vector3.new(direction.X, direction.Y, 0) * 60
bullet.AssemblyLinearVelocity = Velocity

so the bullet heads straight from your barrel to the mouse aim point in 2D

1 Like

i have already done that but forgot about the post. anyways thanks for your detailed explanation