Tank gun stabilization

Hi there,

while creating a tank I`ve stucked on one issue - how to do gun stabilization effect similar to Battlefield or Call of Duty (barrel aims at one point in space, so its pitch varies depending on ground) in blueprints?

The best result was when I modified script from this tutorial, but it wasnt perfect - the barrel constantly aimed at the center of screen. Ive been trying with Line Trace Component, but without effect. I also experimented with Rinterp, but effects were similar to above (barrel aims right at my face :slight_smile:

Thanks for any help, suggestions etc.

I recently calculated hip tilt angle for standing on uneven terrain as part of my IK setup, I believe its a bit similar, so I will try to explain the simple trigonometry which I used, it works perfectly, but maybe there is a simpler way which I don’t know of.
Here it is in BP, (also sync required variable between character BP and animation BP if not doing trace in animation BP.)

EDIT: You don’t need to convert anything into vector for anim graph as in screenshot, only to rotator.

Thanks for reply, it seems to work: for now barrel is stabilized, but constantly returns to base position from beginning - I think it`s not big deal to fix it.

BTW my tank turret and barrel are separate meshes (they will be replaceable).

what do you mean with base position and the beginning, could you post a screenshot and I will have a crack at it?

If you mean, you want the stabilizing effect while being able to aim the barrel, I think there are a few ways of doing that, but what kind of balance do you want? because you could first use animation blends, like aim offsets, or you could use a complicated branch system with a lot of conditions, or you could change the base input values based on your aiming, what exactly do you want to achieve?

I think this might clarify what I mean:
as “Base position” I mean turret position in editor:

[And this movie shows what happens when I try to move barrel up/down (camera is attached to socket in barrel)][2]

Ah I see, the barrel is basically always wanting to return to its position - its because it constantly interpolates the pitch it has to be at depending on the slope, - you could add your control rotation pitch to the pitch currently used to offset the barrel, that would float your barrel’s desired pitch along with your mouse position, I believe effectively providing a way to aim and still neglecting the effect from slope. I am unsure how it would look in blueprints, perhaps you would need to compensate for the mouse aiming in some other way than just adding the pitch, I’m not quite sure.

here is how I calculate mouse pitch btw.