Jump to content
  • Advertisement
Sign in to follow this  
crohnsandme

Trying to get trigger on controller to shoot one bullet at a time

This topic is 1976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all so at the moment if i press the right trigger it will shoot LOADS of bullets(atm they are just drawn no movement yet) but when you use a button you can say if 1 is press and 1 is released then i understand that allows one bullet at a time , but i cannot see how to get this working using a trigger?

 

here is the piece of code i have so far

 

GamePadState gamepadstate = GamePad.GetState(PlayerIndex.One);
            float RightTriggerValue = gamepadstate.Triggers.Right;
            if (RightTriggerValue == 1)
            {
                Shoot();
 
            }
 
please if anyone can point me in the right direction that would be great 
 
thanks

 

Share this post


Link to post
Share on other sites
Advertisement
long currentTimeInMs = clock.getTime(); // whatever the timer is called

GamePadState gamepadstate = GamePad.GetState(PlayerIndex.One);
float RightTriggerValue = gamepadstate.Triggers.Right;

// wait at least 1second(=1000 ms) until you can shoot again
if ((lastTriggerTime+1000)<currentTimeInMs && RightTriggerValue == 1)
{
   Shoot();
  lastTriggerTime = currentTimeInMs ; // lastTriggerTime is a member variable or some other kind of "global" state 
}
Edited by Ashaman73

Share this post


Link to post
Share on other sites

Or another option (to strictly fire once per trigger pull):

 

Keep two GamePadStates, "current" and "previous".

Every frame, push current to previous, and then update current from GamePad.GetState().

 

previousState = currentState;

currentState = GamePad.GetState(PlayerIndex.One);

 

Then only shoot if the trigger is "freshly" pulled, but not if it's being held down.

 

float currentRightTriggerValue = currentState.Triggers.Right;

float previousRightTriggerValue = previousState.Triggers.Right;

 

if(currentRightTriggerValue == 1 && previousRightTriggerValue == 0)

{

    Shoot();

}

 

I've found that creating an inputManager to do comparison checks like these for any button is IMMENSELY helpful.  Then this whole mess can be wrapped in a method call like

 

if(inputManager.IsTriggerJustPulled(Triggers.Right))

{

    Shoot();

}

Edited by BCullis

Share this post


Link to post
Share on other sites

thanks to both of you, i like both methods but what i am after was the current and previous method :) i like the idea of having an input manager as it would be used is probably everything i do although i have to move onto directx and 3D games soon :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!