# Making a 2D gun spin from recoil when it shoots

And here is the code I am using:

if (Input.GetKeyDown(KeyCode.Space))
{
transform.position-recoilPoint.transform.position * recoilForce,
recoilPoint.position,
ForceMode2D.Impulse
);
}


As you can see in the video, the gun is moving too fast and rotating too fast. It seems out of control. I am not sure if the way of adding force to the gun is correct or not.

Thank DMGregory for editing the question for me. I am new to this family so that helped a lot. :)

• How does the current behaviour of this code differ from what you want? Explain in words what should be different. – DMGregory Mar 17 at 17:37

The default rigidbody.AddForce applies the force facing the center of mass of your rigidbody, so it will never directly cause any rotation. But there is also the method rigidbody.AddForceAtPosition(Vector3 force, Vector3 position, ForceMode mode). This method can be used to apply forces to an object which are off-center and thus will result in a mix of rotation and velocity, depending on how far off-center it is.

A good way to use this would be to create a childObject to your gun which represents the point where you want the force to act and then use the position and direction of that object as input for AddForceAtPosition. Here is an (untested!) example script. This script uses a 3d Rigidbody. For a game with 2d physics, replace Rigidbody with Rigidbody2D:

[RequireComponent(typeof(Rigidbody))]
class GunRecoil : MonoBehaviour {

[SerializeField] private Transform recoilPoint;
[SerializeField] private float recoilForce = 10.0f;
private Rigidbody myRigidbody;

private Start() {
myRigidbody = GetComponent<Rigidbody>();
if (recoilPoint == null) {
Debug.LogWarning("You forgot to assign the Recoil Point in the inspector!");
}
}

private void FixedUpdate() {
if(Input.GetButtonDown(KeyCode.space)) {