Search Unity

Making a 2D gun spin from recoil when it shoots

Discussion in 'Physics' started by Franksun418, Mar 29, 2021.

  1. Franksun418

    Franksun418

    Joined:
    Oct 22, 2017
    Posts:
    7
    Hi guys, I am trying to clone the game "Flip the Gun" as an exercise. [
    ]


    Here is the behaviour I have so far
    [https://drive.google.com/file/d/1-v3m69F_MVWQQrvUbENMo0djRnyBVN-D/view?usp=sharing].


    And here is the code I am using:
    Code (CSharp):
    1.     if (Input.GetKeyDown(KeyCode.Space))
    2.     {
    3.         rigidbody2D.AddForceAtPosition(
    4.                     transform.position-recoilPoint.transform.position * recoilForce,
    5.                     recoilPoint.position,
    6.                     ForceMode2D.Impulse
    7.         );
    8.     }
    As you can see in the video, the gun is moving too fast and rotating too fast. It seems out of control. How should i change the code to make the gun spin slower and act just like in the first video? Thank you.