How do you make bullets shoot where your mouse is?

So I used mouse.hit.p for the goal position, and I use

CFrame.new(bullet.Position, mousepos)

To make the bullet face the direction. The problem is that whenever I shoot, the bullet flies the opposite direction of where i’m aiming sometimes. Now notice nothing is in the way of this shot, but it still goes backwards.

https://gyazo.com/8640a1031b0c32f5902c95e3be99b0e4

5 Likes

look at gun tutorials on yt for this answer, make sure to research before posting

2 Likes

Something’s off with how you’re shooting the bullet, but we can’t see without all the code.


Anyway you can get the result you want with the code below:

You can determine what to do when the bullet hits a part, but I’m just making it bounce back in the video above.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if(input.UserInputType == Enum.UserInputType.MouseButton1)then
		--create the bullet when we click the mouse button: 		
		local bullet = Instance.new("Part")
		bullet.Shape = Enum.PartType.Ball
		bullet.BrickColor = BrickColor.new("Black")
		bullet.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-2) -- position the bullet two studs in front of the players character
		bullet.CFrame = CFrame.new(bullet.Position, mouse.Hit.p) --make the bullet face the mouse position
		bullet.Size = Vector3.new(.5,.5,.5) 
		
		--create a bodyforce which combats gravity when shooting
		local antiGravity = Instance.new("BodyForce")
		antiGravity.Force = Vector3.new(0, workspace.Gravity * bullet:GetMass(), 0)
		antiGravity.Parent = bullet
		
		bullet.Parent = workspace
		
		--fire the bullet
		bullet.Velocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.p).lookVector * 100 -- change this number to a smaller number if you want the bullet to go slower
		
		--get rid of the antigravity when the bullet touches a part
		bullet.Touched:Connect(function()
			antiGravity:Destroy()
		end)
	end
end)
22 Likes

It’s because it’s colliding with the camera, I’d use this: UserInputService Mouse V2 - Roblox

2 Likes

@Uralic Can you explain how it works?

it’s mouse, but it’s using raycasting to find the mouse’s hit position. implementation would look something like this: Issues with custom mouse raycasting - #2 by royaltoe
instead of using the mouse object.
it’s nicer because you can ignore items, but it isn’t the issue that we’re having.

1 Like

Works like this, basically you just use it as the normal mouse class or you could use @royaltoe’s solution.

1 Like

How do you make the bullet go the direction with a ray? What if the ray doesn’t hit a part?

I checked the link you sent me to, and the script you made as an example does work, however the problem with it is that when im pointing in the sky or just at an area that has 0 parts where the mouse is pointing, the part doesn’t position itself anymore.

So how does this fix shooting in general? I’m supposed to be able to shoot in the air, and shoot walls and stuff. Why make a script where I have to point my mouse on a part just to shoot?

Add an else statement,

“if the ray doesn’t hit anything, then make the bullet go towards the end of the ray”
the end of my ray is what i put as my second argument in workspace:Raycast

local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local camera = workspace.CurrentCamera
local length = 500

game:GetService("RunService").RenderStepped:Connect(function()
	local unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character, part}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * length, raycastParams)
	
	if raycastResult then
		part.Position = raycastResult.Position
	else 
		part.Position = unitRay.Direction * length
	end
end)
3 Likes

This is all in a local script by the way. What if the bullet’s on the server?

	raycastParams.FilterDescendantsInstances = {player.Character, part}

This piece of code wouldn’t work if it was on the server. Can I use raycastParams on the server, instead of client?

I mean I could just make the goal position a parameter on the server, wouldn’t that be exploitable though?

Why do I have to make the bullet on the client actually? This is confusing.

Um, the devforums only requires you to search the devforums first.

you can make the bullet on the server or the client, it’s up to you. the bullet should only be a visual, it shouldn’t be how you determine where the bullet hits because it can be inaccurate w/ replication.

if you want to make the bullet on the server, send a remote with any info the server needs.

to do the hit detection, determine the mouse position on the client and send that position to the server with a remote event.

the server script then checks if the player can actually shoot their gun to that position by firing a ray from their gun to the given mouse and seeing what it hits.

the player can give a fake mouse position to the server, but you can avoid aim bots by giving your gun accuracy.

But if the bullets only a visual how do I detect if the bullet hits anyone? Aren’t I supposed to use rays from the bullet to determine if it hits someone, or just a wall?

I understand everything else except for how the bullet isn’t meant to damage players, since it’s only made to be seen apparently.

I was told just yesterday by someone that when I make a bullet I should position it with cframing, and use rays to detect if anything comes into contact with it.

Ok but still, its better to research weather its dev forums or youtube or whatever

I use tweening and a bullet script but this is not the “official” way. Most people will tell you it’s bad, but I think it works perfectly well with no lag.

Just use .Touched, it does it’s job.

Is there any way to make this into a tool? Like, instead of it always being active, you have to use a tool (eg. a gun) to shoot? Just wondering… Thanks :slight_smile: