x


key press one a time....

Hi all, this is my 1st answer, and yes I'm new in unity, I'm still make a game where the player move just in X and Z direction, with arrow keys, my problem is: When I press up/down arrow and at same time I press left/right, nothing append, that's ok I want it move up/down, but when I press left/right and I press up/down it change, why? This is the code I put on fixedUpdate. I try also to invert the code, put left/right up on the code and unity invert the situation, but the problem is not solve.

     //Movement
 if (Input.GetKey(KeyCode.UpArrow))
 {
     rigidbody.MovePosition(rigidbody.position + speedH * Time.deltaTime);
 }
 
 else if (Input.GetKey(KeyCode.DownArrow))
 {
     rigidbody.MovePosition(rigidbody.position + -speedH * Time.deltaTime);
 }
 
 else if (Input.GetKey(KeyCode.LeftArrow))
 {
     rigidbody.MovePosition(rigidbody.position + -speedV * Time.deltaTime);
 }
 
 else if (Input.GetKey(KeyCode.RightArrow))
 {
     rigidbody.MovePosition(rigidbody.position + speedV * Time.deltaTime);
 }

I want who play need to release the key to start to move in a different direction, it's a puzzle game :D

I make an online version, this is just the prototype, use arrow and try :D http://www.williamjcrow.altervista.org/file/prova.html

Thanks in advanced

P.S. Sorry for my english, I'm not native ;)

[1]: http://www.williamjcrow.altervista.org/file/prova.htm

more ▼

asked Oct 29, 2013 at 10:10 AM

Corvetti gravatar image

Corvetti
1 3 2 2

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

3 answers: sort voted first

Basically what happens is the fact that every update it checks if you press one of those buttons, because of the order and the else it will stop after the first if that returns true. up will go before down, before left, before right.

to solve this add

 private KeyCode keyPressed;

 Update()
 {
 if (Input.GetKey(KeyCode.UpArrow) && keyPressed == KeyCode.None || keyPressed == KeyCode.UpArrow && Input.GetKey(keyPressed))
 {
 rigidbody.MovePosition(rigidbody.position + speedH * Time.deltaTime);
 keyPressed = KeyCode.UpArrow;
 }
 
 // and add those for the other arrowkeys as well
 
 // then add

 if(Input.GetKeyUp(keyPressed))
 {
 keyPressed = KeyCode.None;
 }
 }
more ▼

answered Oct 29, 2013 at 10:37 AM

thef1chesser gravatar image

thef1chesser
255 3 903 21

I try to put this code, 1st time give me this error Operator '==' cannot be used with a left hand side of type 'UnityEngine.KeyCode' and a right hand side of type 'null'. Then I change null with 0 in all part, I know zero is the default position of the key not pressed, correct? And I don't have error, but when I start the game, player move like I use GetKeyDown like a small movement any time I press, so strange...

Thanks a lot for this solution, I have also tried with a boolean for detect the key pressed, but nothing.

Any other idea ?

Thanks again :D

Oct 29, 2013 at 03:17 PM Corvetti

keyCode is a struct so it cannot be null

Oct 29, 2013 at 03:20 PM fafase

Sorry, I have edited my answer to the correct answer. null should be the KeyCode null object which is KeyCode.None

Oct 29, 2013 at 03:47 PM thef1chesser

But still, this is not working. See you check if the Input up or down or else and is it none. Meaning if not none, not true. What you get is only one frame of movement.

Oct 29, 2013 at 04:02 PM fafase

hmm you are correct... seems i was sleeping

Oct 30, 2013 at 08:35 AM thef1chesser
(comments are locked)
10|3000 characters needed characters left

First wrong decision, the Input in the Update.

Input shoudl be in Update to be checked every frame. Long story short, FU happens at fixed rate, Update happens every frame and your input is generating by OS every frame. If your Update runs 100fps and your FU runs 60fps you have 405 chance your input may be missed.

Now to fix all that.

 enum Previous{Vert, Hor, None}
 Previous state;
 Vector3 velocity;
 
 void Update()
 {
     float hor = Input.GetAxis("Horizontal");
     float ver = Input.GetAxis("Vertical");
     if(hor != 0 && (state == Previous.None || state == Previous.Hor))
     {
          velocity = rigidbody.position + speedH * hor;
          state = Previous.Hor;
     }
     if(ver != 0 && (state == Previous.None || state == Previous.Vert))
     {
          velocity = rigidbody.position + speedV *ver;
          state = Previous.Vert;
     }
     if(hor == 0 && ver == 0)state = Previous.None;
 }


In this case you have to have a moment where all buttons are up. Is it what you are after?

more ▼

answered Oct 29, 2013 at 03:29 PM

fafase gravatar image

fafase
26.1k 66 73 136

alt text

I try, but I have a lot of error, so I think to make this game different, maybe I can do much simple, because is 2 week I try different way but nothing....thanks a lot for your help...

unity.jpg (412.5 kB)
Oct 29, 2013 at 04:49 PM Corvetti

You have something wrong with

 var state = Previous;

it should be

 var state : Previous;

That is why you get all the error after.

Oct 29, 2013 at 05:07 PM fafase

OMG it's true :D sorry, it works :D YEAAAAAAAAAAHHHHHHHHH, I have just to fix get axis because it move just up and right :D so thanks a lot I hope to finish this game soon :D thanks again :D

Oct 29, 2013 at 05:51 PM Corvetti

yes Actually it should read

 if(hor != 0 and rest)
 if(ver != 0 and rest)
Oct 30, 2013 at 08:44 AM fafase

Well, I think also with notequal 0 working great, but it's not like this, with this variation he go always in one direction per axis, down for vertical and right for horizontal. I think I have to make enum for all key and none status. But I have thanks you so much because I learn how to use the enumaration :D I try with all key. Thanks again.

Oct 30, 2013 at 03:37 PM Corvetti
(comments are locked)
10|3000 characters needed characters left

Quite an old question, but I was attempting the same thing and came across a much more streamlined method.

     public float moveSpeed = 10.0f;
     public Vector3 moveVector;
 
     void Update()
     {
         GetInput(Input.GetAxis ("Vertical"), Input.GetAxis("Horizontal"));
     }
     
     void GetInput(float vert, float horiz)
     {
 //apply directional vectors
             moveVector = (transform.forward * vert + transform.right * horiz) * moveSpeed;
 
 //cap speed
             moveVector = Vector3.ClampMagnitude(moveVector, moveSpeed);
 //apply to velocity
             rigidbody.velocity = moveVector;
     }


It eliminates all the if checks. That's a good place to start. I ended up putting acceleration into it so the changing of directions isn't so instant but this method works just fine. You don't have to pass in the Inputs as parameters, that's just what I did. Hope it helps someone.

more ▼

answered Apr 01 at 02:06 AM

dre38w gravatar image

dre38w
85 47 43 48

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

x17

asked: Oct 29, 2013 at 10:10 AM

Seen: 1292 times

Last Updated: Apr 01 at 02:13 AM