How to make an 'aim-able' gun?

Hello everyone! So I’ve had this question for a while and i’m not entirely sure how i could implement this. How would i go about making an ‘aim-able’ gun? I’ve tried looking for tutorials/forum posts on this, but i haven’t really found any.

  • What are you attempting to achieve?

I’m trying to script a gun tool that you can aim down the gun’s sights when holding down the right mouse button.

  • What is the issue?

I’m not entirely sure how else i could do this.

  • What solutions have you tried so far?

One thing I have tried doing is using animations to achieve the aiming, but when i do this, the player looks really weird when aiming and the gun’s sights does not line up with the mouse making the aim not accurate. Edit: I could make it line up with the mouse, but it would be very tedious tweaking the animation ever so slightly. Is there an easier way to do this?

How other players see me doing the aiming animation.
img1

What i see when doing the aiming animation.
img2

Any help will be greatly appreciated! :slightly_smiling_face: (Also first post! :grin:)

13 Likes

You could make your arm invisible and then weld a fake arm to it to make it easier to rotate, it’s an old trick but it’s cheap and easy.

6 Likes

Another idea is that you could probably change the grip of the tool while also moving the arm slightly to make it look better when aiming. You could also just move the head slightly to give the effect of aiming down sights.
You could also just render this locally only in first person, so nobody else sees it, especially since it looks kinda weird when seen by other players.

1 Like

I could do this, but then how could i make the gun’s sights line up with the mouse to make the aim accurate?
(Edited the post with some more info.)

1 Like

I’m slightly confused, what’s the current method that you’re using to make it line up?

1 Like

Currently, with the animation way, i’m just eyeballing it… (It would take forever to get it right at this rate) Which leads me to ask if a better way to do this exists.

3 Likes

One way you could do it is by simply putting the gun locally at the player’s camera center. Then, make it look better on the server-side.

Hope I could help,
-REALTimothy0812

5 Likes

If your game is in third person then you don’t need to have a proper aim down sights system, since that’s kinda useless from the third person.

But if I understood right you are making this so that the gun is used in first person view. In which case, you should try making a View Model that only exists on the client, which you can position in the center of the screen and make it look immersive. This is how all the good FPS games do it.

so a great thing to use for viewmodels is the CFrame:Lerp() property since you can create really nice smooth animations with it.

so heres a small snippet of code that only shows the positioning of the viewmodel and how it can be aimed

  local gripcframe = CFrame.new(0.4,-0.3,-1) -- position to hold the gun
  local aimcframe = CFrame.new(-gripcframe.X,0,0) -- aim offset cframe, Z value is the negated gripcframe.Z Value to return the gun to middle of screen
  local aim = CFrame.new(0,0,0)
  local aimed = true
  local mouse = game.Players.LocalPlayer:GetMouse()
  local viewmodel = (ModelHere) --should have a PrimaryPart Set
  

  mouse.Button2Down:connect(function()
  aimed = true
  end)

  mouse.Button2Up:connect(function()
  aimed = false
  end)

  while true do
  if aimed = true then
  aim = aim:Lerp(aimcframe, 0.3)  -- is player is holding down right click, move gun towards 
  aiming position
  else
  aim = aim:Lerp(CFrame.new(0,0,0), 0.3) --reverse our aiming affect by bringing the aim cframe to 0
  end
  game:GetService("RunService").RenderStepped:wait()
  viewmodel:SetPrimaryPartCFrame(workspace.Camera.CFrame*gripcframe*aim) -- moving the weapon to be in middle of screen, then editing that by our two cframes (grip and aim, but aim dynamically changes its value with right click/right click up)
  end

so this is a great and simple method for making an aim down sights system, but it requires you to use a client ViewModel as WELL as a server model in the characters hands for everyone to see.

This method makes sure that the gun always ends up aligned with the middle of the camera when aiming, and does not require tedious eyeballing to make sure the offset is correct.

If you have any questions message me.

Heres a simple place I made that utilizes this method of Lerping: Streak (Testing) - Roblox

24 Likes

Hooked something similar to this script up to my script and it works like a charm! Thank you!

4 Likes

I’m glad it helped!

If you ever need help with guns and such, that’s my specialty, so feel free to send a message

14 Likes

how do i make a thirdperson aiming system