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.