Search Unity

Question How to add recoil to 2d shot

Discussion in 'Scripting' started by TheSnowyDev, Feb 10, 2021.

  1. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    I'm making a 2d top-down shooter game, and I want to make it so that after the player fires a shot, it has a recoil effect that makes the player glide back slightly using an RB2D.AddForce. the problem is that I tried that, but the way I tried to implement it made it a sudden jump backwards. Any ideas on how to do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Probably easiest to use an animation so you can make it look like any curve you like.

    Alternately if you do it in code, use AnimationCurve objects to drive the rotation and offset.

    Beyond why it makes a sudden jump backwards, sounds like a bug.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    an animation won't work because I don't want the player to return to its original position.
    Here's my current code if you want to take a look at it:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pistol : MonoBehaviour
    6. {
    7.     Rigidbody2D playerRb;
    8.  
    9.     GameObject player;
    10.     [SerializeField] GameObject bulletPrefab;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         playerRb = GetComponentInParent<Rigidbody2D>();
    16.         player = GameObject.Find("Player");
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if(Input.GetButtonDown("Fire1"))
    23.         {
    24.             Shoot();
    25.         }
    26.     }
    27.  
    28.     void Shoot()
    29.     {
    30.         Instantiate(bulletPrefab, transform.position, transform.rotation);
    31.         playerRb.AddForce(transform.up * -2500f);
    32.     }
    33. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    This forum isn't so much about us complete strangers gazing at your code in total isolation and suggesting where it might have gone wrong. Code is only a fraction of the problem; the rest could be scene setup. We don't have that. You have that.

    This is likely the cause of your sudden jerk. As I originally suggested, drive the player back over the course of several frames, perhaps using an AnimationCurve object to specify the drive-back magnitude.

    Alternately, since there really isn't anything recoil-y above except that AddForce command, maybe you should work through two or three different recoil tutorials just to see how they do it.

    Screen Shot 2021-02-17 at 10.04.03 AM.png