- Home /
Input Problems
So I am using a quick powerup system which requires different inputs i want the 'e' button to activate my powerup and then when i click the mouse button i fire 3 balls in a cone. here is my code can anyone help me?
var projectile : Rigidbody;
var angledifference:float = 20;
function Update () {
if(Input.GetKeyDown(KeyCode.E))
{
if (Powerup.currentpowerup == 1)
{
MultiBall();
}
}
}
function MultiBall ()
{
Debug.Log("Running MultiBall");
if(Input.GetMouseButtonDown(0))
{
Debug.Log("Clicking Mouse");
var ballRot= transform.rotation;
ballRot.x += angledifference;
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, ballRot);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * 200);
var ballRot2 = transform.rotation;
ballRot2.x -= angledifference;
var clone2 : Rigidbody;
clone2 = Instantiate(projectile, transform.position,ballRot2);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone2.velocity = transform.TransformDirection (Vector3.forward * 200);
}
}
for some reason it gets to the Runnign Multiball debug line but no further
Re-formatted your code. Nice try but it went a little wrong :P
Answer by Jeff-Kesselman · May 08, 2014 at 01:05 AM
SO.. .this code is only going to work if you hold mouse and press the e button.
Thats because GetKeyDown only fires once per keystroke and, if the mouse button isn't down when it fires and calls Multiball, nothing is going to happen.
If yo uwant to be able to hold the e button and then fire with the mouse while the e button is down, you want to replace GetkeyDown with GetKey. Get key will continue to return true as long as the key is held down.
Finally, if what yo want is for the e key to toggle you into multi-ball mode then you do want GetkeyDown, but you donut want to call multi ball there. Instead you want to set a flag to check inside of your mouse down code to see if its multi ball or not.
I can't give you any more help then that without knowing exactly which effect you a re trying to accomplish.
Answer by robertbu · May 08, 2014 at 01:15 AM
I'm not sure what you want, but here is a try replace Update() with:
function Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
Powerup.currentpowerup = 1;
}
if(Powerup.currentpowerup == 1 && Input.GetMouseButtonDown(0))
{
MultiBall();
}
}
An remove the 'if()' condition on line 22 of the MultiBall script.