Day 23, Gun implementation

Ji Soo Ahn
3 min readDec 17, 2020

--

Shots, Fine sight, and Reload

First was the reload system in the gun. Define a private bool of isReload and add to input.button fire1 && !isReload, to make sure that shots fired is inactive while reloading. When currentBullet < 0, startcoroutine of reload initiates.

Reload Coroutine
Reloading after 3 shots, adds current bullet count into the carry bullet count and from the total, reloads reloadbullet count of 10, resulting 17 carry bullet count remaining to be able to get reloaded at later times.

when bringing in the coroutine, when extra carrybullet count is >0, isReload bool is set to true and bring in the animation. Current holding bullet in the gun is added to the remainder of extra bullets holding, and the amount and from the total bullets remaining then is set to a total of what can be reloaded into the gun. Yield return is brought up to give seconds of time for reload to prevent automatic shooting after the reload is done. Without this, the moment reload is done, there was an automatic shooting initiation where 1 bullet was shot automatically and only 9/10 bullets remaining in the newly reloaded magazine. Then the amount of extra bullet is subtracted to how much was reloaded into the gun. And when the reload is done, the isReload bool is set to false. For where the no bullet Debug.Log is set, I can later bring in the audio file that there are no more bullets remaining and empty magazine shooting sound can be implemented. Also, when the Keydown of R is pressed, it also initiates the reload system so you don’t have to shoot everything in the magazine in order to initiate the reload.

Thus, the reloading the gun comes to this.

Next is the fine sight of the gun. A bool of fine sight is also defined and serializedfield of vector3 original position was set. If we don’t define the vector, it will automatically set the origin of the position to 0, 0, 0. Pressing Fire2 (right click) brings out the fine sight mode and to simplify the bool setting,
isFineSightMode = !isFineSightMode;
currentGun.anim.SetBool(“FineSightMode”, isFineSightMode);
is implemented. when value is isFineSightMode, we start the activatecoroutine and else, we run the deactivatecoroutine.

Fine Sight Coroutine

Within the activate, finesightoriginpos is defined within the gun script as public and not within the gun controller so that every gun can have their own originpos. Then from the scene, we move around the gun to find the right position of where I want the position to be and use the vector3.lerp to make the movement. When deactivating, it returns to the original position within the gunholder in the hierchy. However, when repeated constantly between activating and deactivating, the position kept moving between the Y value as the coroutine conflicts between the two. So before bringing in these two coroutines, we have to stopallcoroutine before calling another, make sure the gun returns to 0,0,0 when returned from the finesightmode. As such:
if (isFineSightMode)
{
StopAllCoroutines();
StartCoroutine(FineSightActivateCoroutine());
}
else
{
StopAllCoroutines();
StartCoroutine(FineSightDeactivateCoroutine());
}
and this fixed the above problem.

Fine sight mode.

I was in a bit of scare when my visual studio gave me a notification that my 30 day trial is ending today. But I found that once you log into the microsoft account, it automatically renews the trial period and there are no need for buying a license to renew, just perfect for a poor developer like me :).

--

--

Ji Soo Ahn
Ji Soo Ahn

Written by Ji Soo Ahn

Software Developer @ GameDevHQ

No responses yet