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

Started by
2 comments, last by crohnsandme 11 years, 2 months ago

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

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 
}

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();

}

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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 :)

This topic is closed to new replies.

Advertisement