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.

enter image description here

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?

  • \$\begingroup\$ 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. \$\endgroup\$ – Philipp Jan 24 at 12:43
  • \$\begingroup\$ Even if I do that, it will only move on x axis. How can I deal with the y axis? \$\endgroup\$ – Kevin Bryan Jan 24 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.