- Home /
How can I check to see if a button combination is pressed?
Hello All...I am trying to create a simple version of a tank like controller. I originally just put in controls using the default inputs for the "vertical" and "horizontal",(arrow keys or WASD). I am pretty new to scripting in general so ... please be gentle. :)
// define variable for the move speed and the rotation speed
var speed = 3.0; var rotateSpeed = 3.0;
function Update () { var controller : CharacterController = GetComponent(CharacterController);
// if A or D is pressed independently then Rotate that direction around the y axis
transform.Rotate(0, Input.GetAxis ("Horizontal") *rotateSpeed, (forward));
// If A and D are pressed simultaneously Move forward
NOT SURE HOW TO DO THIS PART .....
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("BOTH HORIZONTAL INPUTS");
controller.SimpleMove(forward * curSpeed);
}
Thanks for any help you can offer... Grateful Noob...
Answer by Eric5h5 · Jan 18, 2010 at 07:28 PM
var leftDown = Input.GetButton("Left");
var rightDown = Input.GetButton("Right");
if (leftDown && !rightDown)
// Rotate left
else if (rightDown && !leftDown)
// Rotate right
else if (leftDown && rightDown) {
// Move forward
Set up "Left" and "Right" in the input manager. GetAxis isn't useful in this particular case since you need to check the keys individually. You generally don't want to use GetKey unless you intend on writing your own input manager. GetKeyDown (or GetButtonDown) won't work for this, because that only returns true for the one frame when you press the button down.
Answer by m50 · Dec 20, 2009 at 08:00 AM
try:
if(Input.GetKeyDown(KeyCode.A) && Input.GetKeyDown(KeyCode.D)){ controller.SimpleMove(forward speed); } else if(Input.GetKeyDown(KeyCode.LeftArrow) && Input.GetKeyDown(KeyCode.RightArrow)){ controller.SimpleMove(forward speed); }
reason for else if instead of just another if, is because then you could (i think) be moving twice each frame if you held down all 4 keys. this will make it so that if you press A and D then you will go forward OR if you press left arrow and right arrow you will go forward.
another way it can be done (but this is not for sure, its just a maybe) is:
var curSpeed = speed Input.GetAxis ("Horizontal") -(Input.GetAxis("Horizontal"));
use whichever way you want, first way will work for sure, but requires a little extra length, second way im not sure will work, but is a single line.
hope this helps :)
Unfortunateluy ... neither suggestion seems to be working... i try again tonite....
var speed = 3.0; var rotateSpeed = 3.0;
function Update () {var controller : CharacterController = GetComponent(CharacterController); // Rotate around the y axis transform.Rotate(0, Input.GetAxis ("Horizontal") rotateSpeed, 0); // $$anonymous$$ove forward var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed Input.GetAxis ("Vertical"); if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)) { controller.Simple$$anonymous$$ove(forward speed); } else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow) && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) { controller.Simple$$anonymous$$ove(forward speed);
}
The above was my attempt at your first suggestion.n I get no errors, but the tank still wont move forward. $$anonymous$$gestions?
no idea then... I truthfully have no idea... should work, sorry i couldn't of been more use :(
Answer by RevVo98 · Aug 13, 2013 at 12:49 PM
Put a debug statement above your move forward code, see if the block is even being called. If the debug prints something, then the keys are working fine, but your move forward method is failing.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
My script for a "shield" function goes on/off constantly when it's supposed to be on 1 Answer
Camera movement using touch input 0 Answers
How to successfully apply force calling a function from another script 1 Answer
Run Sprint animation and stop it when button is released 1 Answer