transform.rotate

I’m working on recoil for weapons and i decided to use transform.rotate and it works well kinda… i need it to not bounce back to its original position and rather just rotate -5 and then with the next shot rotate another -5. but it bounces back. this is most likely due to me not know the language very well. please help if you can.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class recoil : MonoBehaviour {
    public float fireRate = .16f;
    public float ammo = 20f;
    private float nextFire;
    private WaitForSeconds shotDuration = new WaitForSeconds(0.07f); 
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            ammo -= 1;
            transform.Rotate(-5, Time.deltaTime, 0, Space.Self);
            
        }

        if(ammo == 0)
        {
            ammo = 20;
        }

        if(ammo < 20 && ammo > 0 && Input.GetKeyDown(KeyCode.R))
        {
            ammo = 20;
        }
	}
}

Uh, just use animation for recoil.

Hello @Joeman9832 ,

As Ju_M says, is 100% recomended to use animation. Is more easy than scripting, and you can see the results as you make it. Look some videos to learn if dont know how to do it, but in less than 15 minutes you will know all you need for do it!

:smiley:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public float fireRate = 0.16f;
    public float ammo = 20f;

    // total recoil influence of a fire action
    public float recoilDegrees = 5f;
    // time it will take for gun to reach full recoil
    public float recoilTime = 0.16f;

    // recoil variables
    private float prevFire;
    private float prevRecoil;    
    private float totalRecoil; // a movie by Arnold Schwarzenegger :D

    private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);

    // Use this for initialization
    void Start()
    {
        prevFire = prevRecoil = 0;
        totalRecoil = float.MaxValue;
    }

    // Update is called once per frame
    void Update()
    {
        // get the burrent time
        float time = Time.time;

        // fire if enough time has passed sine last shot
        if (Input.GetKey(KeyCode.Mouse0) && time > prevFire + fireRate)
        {
            prevFire = time;
            prevRecoil = totalRecoil = 0;
            ammo -= 1;
        }

        // if still recoiling then apply recoil
        if (totalRecoil < recoilDegrees) {

            // get the total recoil for this time
            totalRecoil = recoilDegrees * Mathf.Sin(90f * (1f / recoilTime) * Mathf.Min(time - prevFire, recoilTime) * Mathf.Deg2Rad);

            // apply the total recoil less the previous recoil. make it neg to go up
            transform.Rotate((totalRecoil - prevRecoil) * -1f, 0, 0, Space.Self);

            // save last recoil for next loop
            prevRecoil = totalRecoil;
        }

        if (ammo == 0)
        {
            ammo = 20;
        }

        if (ammo < 20 && ammo > 0 && Input.GetKeyDown(KeyCode.R))
        {
            ammo = 20;
        }
    }
}

Thank you so much unit nick