Day 28, Gun Implementation — Recoil, Debugging
Within the shoot(), We are going to implement Recoil of gun system by writing a coroutine. First, I needed to set the Vector3 positions of RecoilBack and also for when FineSight RecoilBack. Current gun already have forces of recoil announced in the script. Thus, a simple, Vector3 recoilBack = new Vector3(currentGun.recoilActionForce, originPos.y, originPos.z); and its similar for the finesight but just changing the currentgun.Finesightforce, the y and z value to finesightOriginPos.y and z. The gun position value is changeable through the x value because I’ve already changed the position of the gun within the player by changing the value in the gun holder to make it face the same way as the player would.
Then, we input a if function to, if !isFineSightMode.
currentGun.transform.localPosition = originPOS;
while(currentGun.transform.localposition.x ≤ currentGun.recoilActionForce — 0.02f)
{
currentGun.transform.localposition = Vector3.Lerp(currentGun.transform.localposition, recoilBack, 0.4f);
yield return null;
}
else
{
… similar as above but changing by the finesightmode values.
}
this way, the 0.02f gives a little leeway to return. And so the gun position will move positive value (back towards the player in this case) and return to the original position value. And of course when this coroutine is run, it needs to have stopallcoroutines(); before to undo multiple coroutines running and messing with the value changes.
Also, within the script there was an error when the player Reloads, you switch to a finesightmode that stops the conflict in mode. So the Reload value gets stuck in true* incompleting the process and thus Fire does not initiate as it does so when !isReload. So in TryFire(), i need to add the value in input.Getbutton && !isReload, add CencelFineSight(); within the Fire();, TryFineSight() also needs to have in input button, &&!isReload, and create
private void CancelFineSight()
{if(isFIneSIghtMode) FineSight();}
This completes and blocks in changing of mode while trying to reload which then prevents the gun being able to shoot as reload value doesn’t get stuck at a value true.