I can't find a way to aim a gun

Hello! Today I am writing about an issue I have had in Roblox for a little while. My problem is, that, I made a gun (which does not yet shoot bullets, I know how to make it shoot.), but I can’t seem to make it point in the direction of the character’s head. Just so it is clear, I am making an fps game and I am not having a problem with making the gun shoot.

SCREENSHOTS



I have searched for solutions online, on the dev hub, and anywhere I could think of. I didn’t find anything

I have no code yet to make the AR aim in the direction of the head, so I hope you can help me find one!

Please do not write out an entire program, but notify me of the possibilities.

Thanks,
@PatitoCeb

2 Likes

Right so there are 2 options: a simple one and a slightly more involved one.

The simple option uses the Mouse object, more specifically, Mouse.Hit to determine where the user is aiming. You then take the Mouse.Hit and do Workspace:Raycast to create a ray from the aim and find any parts which the ray hit (these would be found using RaycastResult.Instance.)
Then you send a RemoteEvent to the server (remember, never take the client’s word that they have hit something - you must always check). You may or may not wish to fire the bullet on the client merely for the sake of visuals as the server processing may take a splitsecond. The server then checks if you actually have ammo, if you are alive, if you exist and if you have line of sight woth the target (again achieved using Raycasting - draw a ray from the player to the target and see if there is a part in the way. If there is, the hit is invalid). Then you simply deal damage to the client who was hit (using a server-sided script, of course).

So, all in all:

  1. client Raycasts from Mouse.Hit
  2. client fires off a RemoteEvent, passing the information of who it hit
  3. the server evaluates if the hit is valid or not (could be an exploiter)
  4. the server damages the player who was shot

The more involved method does not use Mouse.Hit, but UserInputService to find the X and Y coordinates of the mouse and then ScreenPointToRay to convert these into a ray. You can take infotmation from this ray to do Raycasting (2 distinct things). From here, all is the same.

1 Like

Hello! I appreciate the help, but that is not what I asked. I asked if there was a way to point/aim the gun; a bit like the guns in arsenal. As noted in my description, I know how to code the bullet, but I want the gun to follow where the head is looking.

Use a fe gun kit, might work. :slight_smile:

Maybe you could use Mouse.Hit and Hit.p to change the AR’s rotation towards the object, or maybe weld the weapon to the Head if the Head already moves.

1 Like

The head does not already move, but you gave me an idea; what if I made the HumanoidRootPart move?
The only problem is making the Torso move. If anyone knows how to do it, please tell me

EDIT 1: Edited HumanoidRootPart to Torso

Wouldn’t that make the whole player rotate ? I saw a tutorial on how to make an
NPCs head follow a player, you could try stitching some code together to make
your AR aim somewhere.

I’m sorry, I didn’t mean the HumanoidRootPart, but the Torso, since the Torso controls the Head orientation. I’m really sorry about the mistake.

It’s okay, I also think you should use the Neck’s Motor6D to rotate the head. EDIT : Sorry, I thought it was a tutorial at first, I apologize if I confused you more.

Using that kind of program will only rotate the head not the torso, and since a fps game is a “first person” game, it automatically turns the head when looking right to left, the problem is up and down.

EDIT 1: It’s okay

Oh, alright. It’s R6, then ? Maybe try that with what I said earlier.

Please tell me which message you are relating to please

Post 4, I think, my first reply.

Note: I know how to fire and equip the gun