I’m trying to align rifle to camera view direction and have some issues i can’t figure out.
I added a socket to spine03 bone and placed it by the shoulder where the rifle butt-stock should touch the pawn on the TPS pawn’s skeleton.
Then in the TPS pawn construction script, i spawn a empty scene component and attach it to the aforementioned socket.
I also added a socket on the root bone of the rifle placed where the butt-stock should meat with the socket of the TPS pawn.
I then spawn the rifle and attach it to the empty scene component and place it appropriately.
The empty scene comp. is to be used as the pivot point, and using IK, i place the hands at proper location on the rifle.
Now, i’m trying to make the rifle align with the camera using line trace and get look at rotation but its never fully aligned.
Sorry if its a little spaghetti. I tend to do that prototyping and adding/moving/removing nodes a lot.
I clean it up after the prototype is working correctly.
In this SS you can see the red line trace is hitting the stairs and thus miss-aligns vs the blue line trace coming from the camera.
That is fine, as expected even as the cam. line trace offset is beyond possible alignment and thus intersects.
In this SS however, you can see the issues i’m having. The rifle should align with the cam. line trace but it goes off and i’m not sure how or why, and i don’t know how to fix it.
Gun class (Actor) … Scene Component as Root → Skeletal Mesh (gun) as child.
For hand placement I add 1 socket to the gun mesh… Left hand IK (barrel)
On the character mesh I add a socket to the right hand. When I spawn the weapon I attach it to the right hand socket. To tweak the positioning I’ll PIE and adjust the position of the gun in the gun class viewport.
Once the right hand position is tweaked and the barrel is decently aligned with left hand I’ll use IK on the left to get it attached correctly (gun -> left hand IK socket).
For shot alignment in TPP I use a camera line trace and “Find Look at Rotation”. Cameras always center on screen. Thus for shot alignment you need to figure out the offset angle (rotation) from weapon (muzzle) to that center screen projected point.
My weapons shoot projectiles, thus I calculate a projectile transform. The result transform is where I’ll spawn my projectile (barrel tip), but it has a modified rotation to ensure it heads to the crosshair on screen (intersects).
Thanks for sharing, i’ll check it out. Just to clarify tho, i’m not trying to just align the shot from the gun muzzle to target but instead the whole gun.
As seen in Metal Gear Online 2 and i think Metal Gear Online 3. Not sure of the top of my head if some other game is also doing that, i’m sure there’s plenty enough doing the same thing.
But yeah, it’s a clear mistake on my part. I am aligning the gun from it’s attached socket and technically it actually is spot on. What i didn’t think about was that the muzzle is not perfectly centered with the attached socket so shooting a line trace from the muzzle forward shows the slight offset. I need to somehow compensate for that but i got no clue how.
So essentially, i need to take in account where the weapon pivot and muzzle is and align that to the camera line trace hit location. Any pointers how to do that?
I cleaned up and put it into a function. xD
Thanks Rev0verDrive, i applied some solutions from your BP.
](filedata/fetch?id=1776612&d=1592291718)
What should i add to the alignment part to account for the muzzle offset between the weapon socket and the cam hit trace?
As said before, weapon is attached on an empty scene where the stock should meet the shoulder on the character and is meant for relative rotations.
Now some weapons have stocks inline with the muzzle and a simple Find Look at Rotation would work but some guns as shown in the picture has a slight offset between the optimal stock pivot point and the muzzle. I’m trying to rotate the pivot scene so as to align the muzzles towards the target, target being a line trace hit/end location. But i got no clue how to do that in BP. Also, it should be noted that the character can lean so i guess the yaw and pitch should take that into account? I converted the offset into local space, taking the world space from the pivot scene, getting the local offset from the muzzle socket on only the X and Z, having the X as well in case some weapons can have offsets on the X axis as well. How can i get the angle between the pivot scene and the offset location and rotate the pivot scene relative by the amount needed to align the muzzle to the target? Please help.