Smoothing Out Tank Turret Rotation

I’m building a third person tank shooter and am working on implementing rotational control for the turret component of my tank playable character. It works pretty well for large rotations, but for finer movements, the turret seems to snap between small angles instead of fluidly rotating.

Video below shows what I mean.

This is done in C++ with the code below:


The code above takes in the X coordinate of the mouse onscreen, then rotates the turret a certain amount either left or right, depending on where on the screen the mouse is moved to. I’ve tried fiddling with the numbers in the movement algorithm above, but I just cant get it to rotate smoothly in very fine degrees of rotation, making aiming at targets very difficult and unintuitive.

Would love some pointers for how to change my code, or where I should be looking to get a better idea of how to change my code. Thanks in advance for your time.

The problem is probably SetMouseLocation. Why?
Remove it. Place that block somewhere else or bind the mouse zone in a different way.

That and the speed for the Rinterpto set to 2.5f, which may have something to do with smoothness.

I wouldn’t Rinterpto at all. You need a Direct input to follow a mouse.
IF you start to process the value you start to lag behind.

Rotation on Z = Rotation Speed * DetaSeconds
should literally be enough.

If you have to match the position of a cursor there’s more to it obviously.
Assuming the cursor is UMG based, you need to get the location of it on the screen - which won’t match the mouse position anyway.

Given your code.
You have to 180 over the display width to get the ratio.
Multiply ratio * mouse x to get the percentage.
Then the percentage is your degrees of rotation from 0 to 180, which you can’t really use without further processing.
But at least you start thinking in degrees…

Thanks for your help. You pointed me in the right direction and got me thinking. I rewrote the rotation function and now its working perfectly.

Here’s the updated code for future reference to anyone else:

Then instead of calling the function in the tick function, I just called it with

PlayerInputComponent->BindAxis(“Turn Right / Left Mouse”), this, &ATank::RotateTankTurret);

I’m not new to programming, but I’m fresh off the boat in terms of understanding Unreal Engine and its specific functions, so this is all brand new to me still.
But now everything is working as expected. Much thanks!

1 Like