How do I make a characters arms point in the direction of the camera?

Hi! I’m currently trying to make a system that makes the character’s arms rotate following the direction the camera is looking at. My intention is for it to work so that when the character is holding a tool, like a flashlight or, in this case, a scanner, the player can look anywhere and the hand will rotate so it still points directly in front of where the camera is pointing. Not too close and not too far away.

The thing is, the arms seem to rotate using the torso as a pivot, which is not what I’m looking for. I believe that for my desired effect, it would be necessary for the rotation to behave as if the pivot were on the head rather than the torso.

I think it’s important to clarify that I’m using a custom character, but it basically works the same way an R6 character would. I’m not sure if the problem is in my script or if it’s caused by my animation. Either way, I’d like to hear any suggestions for a possible solution, as this problem has become a real headache.

This is the code ive been using

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera

local character = player.Character or player.CharacterAdded:Wait()
local torso = character:WaitForChild("Torso")

local rightShoulder = torso:WaitForChild("Right Shoulder")
local leftShoulder = torso:WaitForChild("Left Shoulder")

local rightC0 = rightShoulder.C0
local leftC0 = leftShoulder.C0

local intensity = 1.5

RunService.RenderStepped:Connect(function()
	local lookVector = camera.CFrame.LookVector
	local pitch = math.asin(lookVector.Y)
	pitch = math.clamp(pitch, math.rad(-60), math.rad(60))

	local armRotation = CFrame.Angles(pitch * intensity, 0, 0)

	rightShoulder.C0 = rightC0 * armRotation
	leftShoulder.C0 = leftC0 * armRotation
end)

Im not really used to working with motor6d and C0 C1


Hi! I’m still a little bit confused on your goal.

First, is the player always going to be in first person? Next, you’re working on the flashlight right? and you just want it to point to whatever the player is looking at? and finally, do you want it to move up and down with the camera as well as side to side? or just side to side.

1 Like

Yes, its in my plans for the player to be in first person all of the time or at least most of it, im using a custom camera controller for the system. Yes im working on the flashlight but what can be seen in the video is my scanner system wich basically works as a tool. Im just looking to make it move up and down since with my first person system i think it would be useless to make it move side to side. The thing is when using for example the scanner, the player makes an animation when holding it so the scanner is placed directly in front of the player’s camera, but when looking down the scanner gets to far away and when lloking upwards it gets too close to the camera, clipping through it. To me it seems like the movement of the hand is “orbiting” the torso wich is probably due to how motor6d works, instead id like for the hand to “orbit” the camera, so when looking down the scanner isint too far away or when looking upwards it isnt as close to the camera, at least this is what i believe would be an option for a solution, but i could be wrong

1 Like

Ohhh, I think I get what you want to do now!

And, I believe I know how to do it. I’m a little busy right now though. But as soon as I can, I will send you an explanation for all of it.

I understand, thanks for offering help! Ill be waiting for a response :smiley:

1 Like

Hey I’m back! Let me just write it all out (it may take a bit lol)

1 Like

Ok. So since you said you weren’t to familiar with motor6ds, I’ll briefly cover them here first because that will be helpful later on. So basically, when you have a motor6d, there is a part0 and a part1. Part0 is the part that is not changed when you animate it and Part1 is the part that is changed. Motor6d’s are set up to be good for character joints. How they did this is that c0 represents the “origin” of part1, offset from part0. Basically, if the c0 has an x position of 5 and the x position of part0 is 3, the x of the “origin” of part1 is 8.

And what is useful is that part 1 pivots around this origin.

C1 represents the offset of part1 from this “origin.” so to build a left shoulder, you set the c0 offset to be slightly left of the torso, and then change c1 to be slightly lower so it still orbits around the c0 (shoulder joint.)

You probably already know this, but its good to make sure your understanding is in tip-top shape.

Now that we’ve covered this, lets start with the flashlight.

For the flashlight, you want it to face what the player is looking at right? In this case. This is what we have to do. First, we have to determine the position of the closest 3d object in line with their camera angle from the position of the camera. Then, we have to use some trigonometry to determine the angle of the flashlight. This is because as long as the flashlight is not in the exact same position as the camera, it will bug out for different lengths of objects. I know this might be hard to imagine, so I’ll attach a visual example below:

PDF.pdf (853.7 KB)
As you can see here, this would work. But if the object is further away still on the same line as the camera angle, the flashlight would still need to be angled differently in order to hit its target:

PDF copy.pdf (1014.5 KB)

There’s the same camera angle, but because of the distance, the same flashlight angle doesn’t work.

So because of this, we need to know how far away the object the player is looking at is from the camera. We can do this with recasting.

Then, all we need to do is some trigonometry math to determine the angle the flashlight needs to be at. We need x and y angles for this (I think) but not z because because we just need to account for the vertical orientation of the light and how much it needs to rotate side to side since its in the hand which is slightly more to the right of the head.

So, the first step is to make sure all the motor 6d stuff is set up properly. The easiest way to do this is probably to animate the flashlight inside the hand, and set c0 to 0,0,0,0,0,0 (therefore, the flashlight will always have the same position and orientation as the hand as long as c1 is 0,0,0,0,0,0, and this is how we will make sure it points in the right place when we animate it.) If you don’t want the flashlight to be completely centered in the hand, then I would suggest that you create another invisible part, and attach that to the hand via motor6ds, and then offset that from the hand and attach the flashlight to that via motor 6ds. and then make sure that the flashlight still has the 0,0,0,0,0,0 on the c0 for attaching to the handle. This will still allow you to make it appear as though the flashlight is offset from the hand, but then we can still reference the other middle part to get position values for calculating.

So at this point we have the x, y, and z position of the point in space the camera is looking at (via raycasting.), and at any point we can get the position of the flashlight via either hand.Position or MiddleInbetweenHandAndFlashlight.Position. Now, we calculate the total distance between the flashlight position and the point. this can be done with the pythagorean theorem (which still works in 3d!) we just subtract the xs, the ys, and the zs, square those each, add them, and get the square root. This would look kind of like:

local FP = Vector3 --flashlight position
local OP = Vector3 --object position

local distance = math.sqrt((FP.X - OP.X)^2 + (FP.Y - OP.Y)^2 + (FP.Z - OP.Z)^2)

Now we have the distance from the flashlight to the object.

Next, we need to find the vertical angle and horizontal angle to rotate the flashlight by.

To do this we use trigonometry.

First, we find the difference in y values by doing:

OP.Y - FP.Y

So if the flashlights y position is 4 and the object is 6, then we get 6 - 4 is 2.

Then, we construct a right triangle with the hypotenuse being the total distance and the opposite side being the y difference. Then we use the equation:

sin(angle) = opposite/hypotenuse

so we divide 2 by the total distance to get the sin and then use the arcsin function to get the angle.

And then we do the similar to get the x rotation.

And finally, we apply these to the c1 to make the flashlight face the right direction!

Now I’ll start writing for the scanner. Also sorry for the huge post lol. Also, there may be some bugs with this as its just an outline, so we may have to bug fix.

1 Like

Ok. Now for the scanner.

As far as I think, you want it to stay the same distance from the camera no matter where they look right?

In that case, we take the vertical angle of the camera and extend a line from it that is as long as the distance you want it to be away from the camera is. Then, we just need to calculate how much to move the hand up and down to put it centered on that line. Then, we can use a similar approach as the flashlight to make it face the camera.

So, to move the hand to the right place, we find the x and y values of the point (projecting from the camera) and the x and y values of the hand. Then, we find the difference by subtracting the x of the hand from the x of the point (so if the hand x is 10 and the point x is 12, then the difference is 12-10 = 2). Then we do the same with the y values, and then add that on the c1 x and the c1 y to shift the hand to the correct position.

And that’s it! If you need any more help with the process feel free to let me know.

also, as a side note, it may be y and z instead of y and x, I usually forget which way is x and z in studio lol

Is that BOB? Nice models unfortunately I do not have the brain capacity to help. Good luck!