Hi, how would I go about finding the rotation needed to apply to the hands to aim the muzzle of my gun at an aim point? i.e. aiming the muzzle at the aim point around the hands pivot point, and finding the new rotation of the hand.
Here’s an image of what’s currently happening, and what I want to happen
If the muzzle is 10 centimeters above the hands pivot point, then aim the hands at a point 10 centimeters below the target.
The general formula is simply: Calculate the transform of the muzzle from the hands, then invert that, then apply that transform to the target.
The only tricky part is that the inverse offset needs to be applied in the hands coordinate system, so transform to world, then inverse transform to target, before applying.
You can calculate it using a little trig:
A → Distance trace to target
a → 90 deg
C → Measure the elevation. In my example 50.0
B → Find missing side of triangle: sqrt( pow(A, 2) - pow(C, 2) )
b → Find angle using Sine: acosd(B/A)
Hmm, mine doesn’t seem to work AimAt - YouTube
Also, how would I incorporate the Yaw into it, because the target moves up, down, left, and right within a circle at a fixed distance away from the camera (the line trace setting the distance in the video was just to test your example with a dynamic distance, but as you can see that isn’t working for some reason.)
In case it’s important (for relative rotations and axis, etc…), I’m using a virtual bone for the hand_r, parented to the head, and Replacing VB_hand_r’s Rotation in Parent Bone Space (with ‘Transform (Modify) Bone’) and snapping the hand_r to the VB_hand_r with ‘Two Bone IK’
You have to figure it out with how your hierarchy is set up.
It’s the same math, just test it with yaw.
I don’t know if this is the best way to go about this. If it’s for main weapon, wouldn’t it be easier to just have the weapon lined up with the socket / bone that’s doing the pointing?
I’m not sure what you mean? The weapon is attached to the weapon socket, which is attached to the hand, which is where I’m applying the rotation to point it at the target (well I’m applying it to the VB_hand_r, but the hand_r is copying the transform)
I mean I can’t guess how it is oriented.
You are assigning its Z location as the elevation of the weapon. Idk if that’s correct or not because it depends on how it is all set up.
The math expression I did is correct and it will work once you’ve figured out what goes where.