Please help! How to make your weapon fire in 3 round burst? Created 2 months ago2024-08-27 00:04:11 UTC by zhushengjie zhushengjie

Created 2 months ago2024-08-27 00:04:11 UTC by zhushengjie zhushengjie

Posted 2 months ago2024-08-27 00:04:11 UTC Post #349089
I'm trying to make a fire selector for my mp5(when you press right mouse button,it ,will change fire mode from fully-automatic to 3 round burst,then to simi-automatic),I have already done the fully-automatic part and simi-automatic part,but don't know how to make the 3 round burst.Can someone help me?
Posted 2 months ago2024-08-27 09:57:17 UTC Post #349096
Hi and welcome to TWHL!
Now I haven't dabbled much in the weapons related game code so I'm sure someone else might have a better way of doing it than I.

The way I'd do it is to give new members of type int to CMP5's header; int m_iMaxBurst and m_iBurst and initialise both of these to 3 in CMP5's Spawn() method.

Then in the PrimaryAttack() method, add a decrement to m_iBurst just below the m_iClip decrement, and further down add a check for if m_iBurst is exhausted, and if it is then add a 0.5 second delay to the next fire property and reset m_iBurst to m_iMaxBurst.

At the top:
void CMP5::PrimaryAttack()
{
    // don't fire underwater
    if (m_pPlayer->pev->waterlevel == 3)
    {
        PlayEmptySound();
        m_flNextPrimaryAttack = 0.15;
        return;
    }

    if (m_iClip <= 0)
    {
        PlayEmptySound();
        m_flNextPrimaryAttack = 0.15;
        return;
    }

    m_pPlayer->m_iWeaponVolume = NORMAL_GUN_VOLUME;
    m_pPlayer->m_iWeaponFlash = NORMAL_GUN_FLASH;

    m_iClip--;
    m_iBurst--;

    // ... rest of code
At the bottom:
    // ... rest of code

    m_flNextPrimaryAttack = GetNextAttackDelay(0.1);

    if (m_flNextPrimaryAttack < UTIL_WeaponTimeBase())
        m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.1;

    if (m_iBurst < 1)
    {
        m_flNextPrimaryAttack += 0.5;
        m_iBurst = m_iMaxBurst;
    }

    m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + UTIL_SharedRandomFloat(m_pPlayer->random_seed, 10, 15);
}
You'd then need to make a check if the fire button had been released in the middle of a burst to reset m_iBurst back to m_iMaxBurst.
Posted 2 months ago2024-08-27 13:39:16 UTC Post #349100
Thanks for your reply!
I tried this method and make a little change in it.Now it can fire in 3 round burst.
However,it still a little difference to what i expected.
Now it needs to press and hold mouse left button to fire 3 shots,what i really want is when i click the button,it will fire 3 shots(Just like the Glock in counter-strike).Can you help me?
Posted 2 months ago2024-08-27 15:43:37 UTC Post #349101
I guess you could create a flag m_fInBurst (set to false in Spawn()) that you set to true in PrimaryAttack() if m_iBurst is equal to m_iMaxBurst, and set to false if m_iBurst is zero.
If m_fInBurst is true, set m_flTimeWeaponIdle to 0.1.

Then in WeaponIdle() do something like
void CMP5::WeaponIdle()
{
    if (m_fInBurst && m_iBurst > 0)
    {
        PrimaryAttack();
        m_flTimeWeaponIdle = 0.1;
        return;
    }
    // ... rest of code
Essentially use PrimaryAttack() to start the burst fire, and let WeaponIdle() as a loop to call PrimaryAttack() until the burst count is exhausted.

Again, I'm just guessing here.
Posted 2 months ago2024-08-29 12:50:01 UTC Post #349111
I tried this method,but somehow it seems doesn't work well.Everytime I press the mouse left button,it will fire until bullet is exhausted.after reload it will shoot again without any control.
I found something interesting while looking at glock.mdl in counter-strike.The shoot1 and shoot2 sequence is 3 round burst while only shoot3 sequence is simi-automatic.Maybe similarly to it we can create a new burst animation then use it in the code?(Like when pressing the button execute the PrimaryAttack( ) three times but only play the burst animation once?)
You must be logged in to post a response.