I’ve created a weapon recoil script where when you hold down left click, a variable number gradually increases by 2.5 per second. While this number increases i want an empty to translate upwards by the variable number. Once you release left click the variable number slowly decreases by 3 per second, and the empty moves back down gradually. The max height is 4, and the minimum is 0.
I dont want the empty object to snap from position a to b, i want it to slowly go up by float values.
public float RecoilPerSecond = 1.25f;
public float RecoilRecovery = 0.30f;
public int MaxRecoil = 4;
public float MinRecoil = 0.0f;
public bool IsFiring = false;
public float CurrentRecoil = 1f;
void Update () {
//When holding left click call recoil multiplier and cancel recoil reducer
if(Input.GetMouseButtonDown (0)){
IsFiring = true;
if(IsFiring == true){
recoilRepeater();
CancelInvoke("minRecoil");
}
}
//When releasing left click call recoil reducer script and cancel recoil multiplier
if(Input.GetMouseButtonUp(0)){
IsFiring = false;
CancelInvoke("recoilMultiplier");
minCheck();
}
}
//Function to increase recoil every 0.1 seconds by 0.75
void recoilMultiplier (){
RecoilPerSecond *= 1;
CurrentRecoil += RecoilPerSecond;
if (CurrentRecoil >= 4) {
CurrentRecoil = 4;
}
}
//Constantly repeating recoil multiplier
void recoilRepeater (){
InvokeRepeating ("recoilMultiplier", 1, 0.10f);
}
//Function to reduce recoil every 0.1 seconds by 0.3
void minRecoil (){
CurrentRecoil -= RecoilRecovery;
if (CurrentRecoil <= MinRecoil) {
CurrentRecoil = MinRecoil;
}
}
//Constantly repeating recoil reducer
void minCheck (){
InvokeRepeating ("minRecoil",1,0.10f);
}
}