Help| Recoil per shoot

hey there,
i have that script:

var recoilMod : Transform;

var weapon : GameObject;

var maxRecoil_x : float = -20;

var recoilSpeed : float = 10;

var recoil : float = 0.0;

 

function Update() {

    if(Input.GetMouseButtonDown(0))
    {
	        //every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
	        recoil+= 0.1;
	}
    recoiling();
}

 

function recoiling() {

    if(recoil > 0)

    {

        var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);

        // Dampen towards the target rotation

        recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);

        weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;

        recoil -= Time.deltaTime;

    }

    else

    {

        recoil = 0;

        var minRecoil = Quaternion.Euler (0, 0, 0);

        // Dampen towards the target rotation

        recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);

        weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;

    }

}

the problem is the recoil effect everytime i clicking on my left mouse, and it’s moving just one time, but i want that it will move backward and back to the orginal position and then backward and back to the orginal position and then backward and back to the orginal position, a lot of times…
how can i do that?

UP

UP.

UP…

Google cannot translate your question, please rephrase…


the recoil happed every time i clicking on the left mouse button, just one time,
No matter I press the button for a long time, it happens only once.
I want it to happen with a single click, a lot of times.

UP.

UP.

Set a boolean to true on mousedown and set it to false on mouseup.

You’re over thinking this way to much. I would send a simple message to your camera look script to increase the y rotation when you fire a shot and then simply return to the original rotation using the provided unity functionalities such as lerp. Hope this helps.

please give an example. :frowning:

UP.
i don’t want to use the mouse look script, i want to use that script so please tell me how :).

UP.
please help me.

Obviously its clear in your mind what you want, for some reason, its not clear for me :slight_smile: Anyways, i suppose you want your weapon recoil after shot, and prevent player to shoot again until the weapon is back to aiming position? You need flag to check if recoil animation is done:

var recoilMod : Transform;
var weapon : GameObject;
var maxRecoil_x : float = -20;
var recoilSpeed : float = 10;
var recoil : float = 0.0;

function Update() {
    if(Input.GetMouseButtonDown(0)  waitForAni == false)
    {
           //every time you fire a bullet, add to the recoil.. of course you can probably set a max recoil etc..
            recoil+= 0.1;
            waitForAni = true;
    }
   
    recoiling();
}

private var waitForAni:boolean = false;
function recoiling() {
    if(recoil > 0)
    {
        var maxRecoil = Quaternion.Euler (maxRecoil_x, 0, 0);
        // Dampen towards the target rotation
        recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, maxRecoil, Time.deltaTime * recoilSpeed);
        weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
        recoil -= Time.deltaTime;
    }
    else
    {
        recoil = 0;
        var minRecoil = Quaternion.Euler (0, 0, 0);
        // Dampen towards the target rotation
        recoilMod.rotation = Quaternion.Slerp(recoilMod.rotation, minRecoil,Time.deltaTime * recoilSpeed / 2);
        weapon.transform.localEulerAngles.x = recoilMod.localEulerAngles.x;
    }
    if (recoilMod.rotation == minRecoil) waitForAni = false;
}

I am not sure if its correct way to check when the rotation is back to normal like i did in example:
recoilMod.rotation == minRecoil

If that doesnt work, you need to figure out a way to check when your weapon is back to normal rotation. Hope you get at least some new thoughts.

try this, dont really know if this is what you want…

c# example:

	public float roundsPerMinute = 700f;  	//Rounds Per Minute
	
	private float _timer = 0f;
	
	void Update () {
		
		if(Input.GetMouseButton(0)){
			_timer += Time.deltaTime;
			
			if(_timer >= 60f / roundsPerMinute){
				Debug.Log("Shooting");
				_timer = 0;
			}	
		}
		
	}

again, i want to recoil(gun moving forward and backward) when i’m shooting.
now the problem is, If the fire button is still pressed, the gun moving forwrd and backward just once until the next click, but i want to make that it will move forward and backward not just once but a lot until i will stop the pressing…
now, what you can’t understand…?

you need to loop the animation for as long as the button is down.

but i’m now using animation… -_-
that’s a script. :slight_smile:

//Use this code

function Update () {
animation.Play();
}

lol