- Home /
Power up weapon shot before releasing and resetting power
What would be the code so that holding down a button will charge up shot,then releasing that button fires the charged shot and resets the charge; as used in games like r-type?
Answer by Peter G · Aug 13, 2010 at 04:30 PM
I would do something like this. It starts charging when the user clicks the fire button and will release when they let go.
var chargeLevel : float = 0; //Don't change this in the inspector. var chargeSpeed : float = 1; //Default, the charge will go up 1 per second var isCharging = false;
function Update () { if (Input.GetButtonDown("FireButton")) { //Did the user click? if(!isCharging) { //Some what unnecessary due to the way the Input is // setup isCharging = true; CalculateCharge(); } } }
function CalculateCharge () { while(Input.GetButton("FireButton")) { //Add to the charge as long as the // user is holding the button chargeLevel += Time.deltaTime * chargeSpeed; yield; //Will cause a crash without this. }
//Fire Projectile
// Reset the vars.
chargeLevel = 0.0;
isCharging = false;
}
Yes, thats what i was getting - crashes with while loops, thanks
Hey dude thanks for the idea. I appreciate that :thumbs_up:
Your answer
Follow this Question
Related Questions
Error CS1525: Unexpected symbol `}' 1 Answer
(Solved) Continuous movement 1 Answer
RayCast "blueline" qoes down 1 Answer
simple shooting script help 3 Answers
Shoot script help 1 Answer