Sign in to follow this  

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

This topic is 1760 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
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

This topic is 1760 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this