x


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...

more ▼

asked Dec 20 '09 at 04:09 AM

Shadyfella13 gravatar image

Shadyfella13
356 8 8 22

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Jan 18 '10 at 07:28 PM

Eric5h5 gravatar image

Eric5h5
102k 50 165 622

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 20 '09 at 08:00 AM

m50 gravatar image

m50
76 2 3 5

Unfortunateluy ... neither suggestion seems to be working... i try again tonite....

Dec 20 '09 at 04:32 PM Shadyfella13

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); // Move forward var forward = transform.TransformDirection(Vector3.forward); var curSpeed = speed * Input.GetAxis ("Vertical"); 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); }

Dec 24 '09 at 08:25 AM Shadyfella13

The above was my attempt at your first suggestion.n I get no errors, but the tank still wont move forward. Suggestions?

Dec 24 '09 at 08:26 AM Shadyfella13

no idea then... I truthfully have no idea... should work, sorry i couldn't of been more use :(

Dec 27 '09 at 09:57 AM m50
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 13 '13 at 12:49 PM

RevVo98 gravatar image

RevVo98
3 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x7826
x1364

asked: Dec 20 '09 at 04:09 AM

Seen: 4207 times

Last Updated: Aug 13 '13 at 12:49 PM