Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 crohnsandme   Members   -  Reputation: 123

Like
0Likes
Like

Posted 18 February 2013 - 08:14 AM

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

 



Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7504

Like
1Likes
Like

Posted 18 February 2013 - 08:21 AM

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, 18 February 2013 - 08:24 AM.


#3 BCullis   Crossbones+   -  Reputation: 1813

Like
1Likes
Like

Posted 18 February 2013 - 09:52 AM

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, 18 February 2013 - 09:54 AM.

Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#4 crohnsandme   Members   -  Reputation: 123

Like
0Likes
Like

Posted 18 February 2013 - 11:51 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS