Weapon Recoil via c# script

Hi, if anyone can help out with this. I have a gun script written here that I got from a Brackey’s video. I have continued to build on top of it and have added numerous elements. One thing I am stuck on however, is the weapon recoil. Initially I had it set up to play through the animator controller, but I am trying to set it up via c# script. I will post the entire script if that helps see everything together. Under the “shoot” section I added in a translation movement that moves the gun back whenever I shoot. The issue I am facing is how to move the gun BACK to it’s starting point, simulating the full recoil. I found a tutorial that explains how to move an object back and forth, however that is dependent on the function updating every frame (continuous). Since I am only calling this when I shoot, that doesn’t work. The gun will move back and stay there. Below is the script, any feedback would be great, thanks!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GunScript : MonoBehaviour {

    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 15f;
    public float impactForce = 30f;

    public int magazineSize = 10;
    public int currentAmmo;
    public int magazineCount;
    public float reloadTime = 1f;
    private bool isReloading = false;

    public Camera fpsCam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;

    private float nextTimeToFire = 0f;
    private AudioSource mAudioSrc;

    public Animator anim;

    public Text ammoDisplay;
    public Text magazineDisplay;

    private int totalAmmo;

    public GunScript gunScript;
    public DryFire dryfireScript;

    public float recoilSpeed;

    private Vector3 newPosition;
 
    // Use this for initialization
    void Start ()
    {
        if (currentAmmo == -1)
        currentAmmo = magazineSize;

        mAudioSrc = GetComponent<AudioSource>();
        anim = GetComponent<Animator>();
     
    }



    // Update is called once per frame
    void Update()
    {

        dryfireScript.enabled = false;

        totalAmmo = currentAmmo + magazineCount;
        ammoDisplay.text = currentAmmo.ToString();
        magazineDisplay.text = magazineCount.ToString();

        anim.SetBool("isShooting", false);
     
        if (isReloading)
        return;

        if (currentAmmo <= 0)
        {
            StartCoroutine(Reload());
            return;
        }
     
        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
         
            mAudioSrc.Play();
        }
     
        if(currentAmmo + magazineCount == 0)
        {
            gunScript.enabled = false;
            anim.enabled = false;
            currentAmmo = 0;
            magazineCount = 0;
            dryfireScript.enabled = true;
        }
     
    }

    IEnumerator Reload ()
    {
        isReloading = true;
        Debug.Log("Reloading...");

        yield return new WaitForSeconds(reloadTime);
        currentAmmo = magazineSize;
        isReloading = false;

        magazineCount --;
    }

    void Shoot ()
    {
     
       transform.Translate(Vector3.forward * recoilSpeed * Time.deltaTime);

        currentAmmo--;
        muzzleFlash.Play();
        anim.SetBool("isShooting", true);

        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
        {
            Debug.Log(hit.transform.name);
         
            ZombieHealth target = hit.transform.GetComponent<ZombieHealth>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }


            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }

            Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
        }

    }
 
}

please edit your post and use code tags, it’s the first icon next to ‘Code:’ on the ribbon. more info .