# Libgdx Recoil Effect

I set -velocityX to my sprite gun.

float velocityX = -20 * dt;
if(trigger) {
gun.setX(gun.getX() + velocityX);
}


I basically set a velocity of -20m/s to my gun vertex X. The problem is when I rotate the gun in a vertical position or example: 30degree angle.

How can I add recoil effect even if the sprite is not in a horizontal position, or should I just add it in the animation?

• You want to move the recoil alone your gun's x-axis. Your function doesn't take this into account and just keeps moving it along the x-axis of the world coordinates. An idea would be to have a recoil function and in order to properly calculate the recoil you have to calculate the gun's x-axis in world coordinates and move it then accordingly. – Philipp Jan 24 at 12:43
• Even if I do that, it will only move on x axis. How can I deal with the y axis? – Kevin Bryan Jan 24 at 15:13