How do you make a part move in the direction that the mouse clicked?

So I am trying to make a gun, but I ran into a problem. I don’t know how to make the gun move in the direction that the player clicked. I tried looking for something to help me with this on google but came up with nothing.

2 Likes

You can change the CFrame to make it look at a point.

Part.CFrame = CFrame.new,(Part.Position,Mouse.Hit.P)

We will first aim the part towards the mouse click and then have a velocity towards where we clicked.

-- I suggest instead retrieving the position at the time of clicking, instead of making it a variable.
local mousePosition
local barrelPosition 

local speed = 5

local function getCFrame(startPos, toPos)
   return CFrame.new(startPos, toPos)
end

bullet.CFrame = CFrame.new(barrelPosition, mousePosition)
bullet.AssemblyLinearVelocity = Vector3.new(1,0,0) * speed

Note, The Vector3’s Z could need to be 1 instead of the X.

How would I use that in this script?

script.Parent.Activated:Connect(function()
    local Bullet = Instance.new("Part")
    Bullet.Parent = game.Workspace
    Bullet.Position = script.Parent.Handle.Position
    Bullet.Size = Vector3.new(1,0.25,0.25)
    Bullet.Material = Enum.Material.SmoothPlastic
end)

You would just change the CFrame if the gun, ive given you the basis all you have to do is change it for your use.

(It will most likely just be out in there before firing)

So, like this?

script.Parent.Activated:Connect(function(Mouse)
    local Bullet = Instance.new("Part")
    Bullet.Parent = game.Workspace
    Bullet.Position = script.Parent.Handle.Position
    Bullet.Size = Vector3.new(1,0.25,0.25)
    Bullet.Material = Enum.Material.SmoothPlastic
    Bullet.CFrame = CFrame.new(Bullet.Position,Mouse.Hit.P)
end)

Give it a test, that will tell you. It should be fine like that.

On a side note, is it wise to fire a bullet from the handle?? Firing from the end of the gun would be better

Ok, so I just tested it and it didn’t work. Is there another reason this could be happening.
Yes but I couldn’t figure out how to make it shoot out of the barrel because the tool doesn’t have a Position property.

Change the starting point of the bullet to the lookvector of the barrel, it may be firing from the gun but you need to remember they are seperate entities.

The tool itself doesnt have a position, but any parts in it do.

Use lookAt bc CFrame.new to face something is deprecatd.

There are no parts in it. All the parts are Unioned.

Unions have position and cframe, they are just a different type of part.

So you’d just get the mouse and its position. Then you can subtract the mouse position with the part position then use unit.

Easiest, yet also a semi unreliable solution would be to start out by getting the Guns CFrame, setting the bullets CFrame to the Guns CFrame * the offset for the barrel (So basically get a CFrame which would be where the barrel of the gun is)

From there get the mouse position using either UserInputService or by using the Mouse object from a local player and get it’s Hit position. From there create a CFrame based on those two values with the guns cframe being the primary and the second parameter (the lookAt) being the Hit position from the mouse.

After that you can add a bodyVelocity or other bodyMover object and put it in the bullet object, change the settings so it’s the speed you want and then parent the bullet to the workspace and let it fly. Make sure to add something such as adding the bullet to debris service so after a while the bullet will be removed.

Can provide a code example if requested

1 Like

Yes please. Because I can not understand fully what you said there about the bullets Cframe.

Of course.

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

--Define the scripts parent to be the gun (This is assuming the parent is the gun TOOL)
local gun = script.Parent

--Defining the handle to get the position
local gunHandle = gun.Handle

--Variable for movement speed
local velocity = 100

gun.Activated:Connect(function()
	local mousePos = mouse.Hit.p
	
	--Creating the bullet
	local bullet = Instance.new("Part")
	bullet.Size = Vector3.new(0.25, 0.25, 1)
	bullet.Material = "SmoothPlastic"
	
	--Set the initial cframe to the gunHandles CFrame (Should also account for offset or it will just come out of the handles origin)
	bullet.CFrame = CFrame.new(gunHandle.CFrame.p, mousePos)
	
	--Create body mover object, in this case a bodyVelcocity and set up its settings
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.Velocity = bullet.CFrame.lookVector * velocity
	bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVelocity.Parent = bullet
	
	--Set bullets parent to workspace last
	bullet.Parent = workspace
	
	--Set the bullet to dissapear after 2 seconds
	game.Debris:AddItem(bullet, 2)
end)

Didn’t get to test this code out but it should work. Also I recommend for the future instead of creating parts in scripts to instead create them beforehand and store them in a folder in ReplicatedStorage as it’s faster to clone then to create and set properties.

Also keep in mind since this is all being done locally it will ONLY work locally meaning only the player who fires the gun will see it. If you want to change this then fire a remote-event and have the server do the bullet creation/firing.

EDIT: Made a small change to the size in order for it to be sized in the right dimension. Also tested this code in a tool and works fine. Again though if you want this to be server side rather than client side make sure you use a remote event

9 Likes