- Home /
Divide a number once in update
This is my Aim Down Sights code, and i want when i ads my recoil divide by 3. But it divide by 3 every frame until it reach 0. what i want is: if recoil is 9 it stay 3 on button fire2 pressed
the AdsController function is called on Update
someone can help?
void AdsController() {
Vector3 adsRecoilKickBack = currentWeapon.recoilKickBack / 3;
Vector3 adsRecoilRotation = currentWeapon.recoilRotation / 3;
if(Input.GetButton("Fire2")) {
isAiming = true;
AdsAnimHolder.transform.localPosition = Vector3.Lerp (AdsAnimHolder.transform.localPosition, currentWeapon.currentSight.adsPos, 0.25f);
currentWeapon.recoilKickBack = adsRecoilKickBack;
currentWeapon.recoilRotation = adsRecoilRotation;
}
else {
isAiming = false;
AdsAnimHolder.transform.localPosition = Vector3.Lerp (AdsAnimHolder.transform.localPosition, Vector3.zero, 0.25f);
}
}
If you only want something changed if "Fire2" is pressed then only change it if "Fire2" is pressed, i.e. within the IF clause code block.
If you only want to change the recoil if the current magnitude of the recoil is approximately 9 then just add another IF to check for this:
if ($$anonymous$$athf.Approximately(9, currentWeapon.recoil$$anonymous$$ickBack.magnitude)) {
// Change recoil here
}
You probably don't want to store the temporarily adjusted recoil on the currentWeapon itself, otherwise you have to remember to store the original value and always set it back (not to mention that the weapons internal state should only be managed by the weapon, not a third party script). If the weapon will always have 1/3 recoil when firing (or whatever you are doing) then maybe set and unset a boolean flag on the weapon script that will adjust the recoil... something like:
public bool IsFiring = false;
private Vector3 recoil$$anonymous$$ickBack;
public Vector3 Recoil$$anonymous$$ickBack {
get {
if (IsFiring)
return recoil$$anonymous$$ickBack * recoilReduction;
else
return recoil$$anonymous$$ickBack;
}
}
float recoilReduction = 1f/3;
This way the weapon script itself manages it's state and properties which is much better (low coupling) design.
Thanks for your answer, but isnt what im looking for :C
What i want is to recoil$$anonymous$$ickback divide per 3 and stay divided per 3
what hapens is that recoilkickback divide per 3 each frame until it reaches 0
Answer by smallbit · Jul 18, 2014 at 03:43 AM
You need to store your variables out of Update loop ( and methods called there too). Declare your variables globally :
Vector3 adsRecoilKickBack;
Vector3 adsRecoilRotation;
Than after you switch weapon adjust their values according to current weapon parameters; Call this method when you switch weapon:
void UpdateWepData()
{
adsRecoilKickBack = currentWeapon.recoilKickBack / 3;
adsRecoilRotation = currentWeapon.recoilRotation / 3;
}
Finally remove two lines from your AdsControllerMethod;
void AdsController()
{
if (Input.GetButton("Fire2"))
{
isAiming = true;
AdsAnimHolder.transform.localPosition = Vector3.Lerp(AdsAnimHolder.transform.localPosition, currentWeapon.currentSight.adsPos, 0.25f);
currentWeapon.recoilKickBack = adsRecoilKickBack;
currentWeapon.recoilRotation = adsRecoilRotation;
}
else
{
isAiming = false;
AdsAnimHolder.transform.localPosition = Vector3.Lerp(AdsAnimHolder.transform.localPosition, Vector3.zero, 0.25f);
}
}
Should work that way.
Your answer
Follow this Question
Related Questions
Update() Function only fires once. 1 Answer
Check if key is pressed once 1 Answer
How to run a update function once 3 Answers
Calling a function only once in Update 6 Answers
Call A Function At A Certain Time 1 Answer