- Home /
issue with the input keys
I'm trying to make my game character shoot when pressing space. the problem though is that he only wants to shoot when i press space and the left or right key at the same time. could i get some help with this issue please? also, here's some code for you:
function Update () {
var turning = Input.GetAxis("Horizontal");
var fireAtWill = Input.GetButton("Fire2");
if(turning){
gameObject.rigidbody2D.AddTorque(turning*turnVal);
if (fireAtWill || Input.GetKeyDown("space")){
Instantiate(bullet1, Vector3(transform.position.x+0.82, transform.position.y + 2.5, transform.rotation.z), transform.rotation);}
Instantiate(bullet1, Vector3(transform.position.x-0.82, transform.position.y + 2.5, transform.rotation.z), transform.rotation);}
}
thank you.
There are closing braces, they're just at the end of lines 9 and 10.
The closing brace on line 9 needs to be moved to line 7.
Answer by TheRichardGamer · Apr 15, 2014 at 07:46 AM
Using a input in a variable isn't a great method of using input, instead use mouse button like this:
function Update () { var turning = Input.GetAxis("Horizontal");
if(turning){ gameObject.rigidbody2D.AddTorque(turning*turnVal);
if (Input.GetMouseButtonDown(2) || Input.GetKeyDown("space")){ Instantiate(bullet1, Vector3(transform.position.x+0.82, transform.position.y + 2.5, transform.rotation.z), transform.rotation);} Instantiate(bullet1, Vector3(transform.position.x-0.82, transform.position.y + 2.5, transform.rotation.z), transform.rotation);}
}
Your answer
Follow this Question
Related Questions
jumping at a specific height (using rigid body) 1 Answer
hard to explain but, when my game is paused my space bar goes weird. 0 Answers
Unity 4.3 - Spacebar to Full Screen? 2 Answers
how to make my game object to be at mouse position and then to fall at that position? 1 Answer
How to make a dice fall when space bar is pressed. 3 Answers