Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GridStyle Movement without a Grid

Discussion in '2D' started by Sedamnest, Aug 3, 2016.

  1. Sedamnest

    Sedamnest

    Joined:
    Aug 3, 2016
    Posts:
    3
    Hello, I'm having a problem with some basic movement here :/

    my goal to achieve is a movement similar to pokemon, where you can only move in one direction at a time - Up, Down, Left and Right. (no diagonal movement)

    My solution doesnt work very effectively as soon as 2 arrowkeys are pressed at once. it behaves like follows:

    When pressing left or right, while holding the up or down key he correctly changes the direction to the latest button pressed.

    But when I press up or down, while holding the left or right button, he ignores the second buttonpress completely and continues walking left/right.

    I guess the problem lies somewhere in priorities over the if statements or similar..tried a few things, with more if statements, witch a switch statement, adding a bool and even another statemachine but somehow i cant get the desired result :'D
    Hope you guys can help me, here's the code:

    codes in the Update() function

    Code (CSharp):
    1. void UpdateTransform()
    2.     {
    3.         //Right
    4.         if (horzInput > 0)
    5.         {        
    6.                 transform.Translate(Vector3.right * walkSpeed * Time.deltaTime);
    7.                 SetOrKeepState(State.WalkingRight);
    8.         }
    9.         //Left
    10.         else if (horzInput < 0)
    11.         {
    12.                 transform.Translate(Vector3.left * walkSpeed * Time.deltaTime);
    13.                 SetOrKeepState(State.WalkingLeft);
    14.         }
    15.         //Up
    16.         else if (vertInput > 0)
    17.         {
    18.                 transform.Translate(Vector3.up * walkSpeed * Time.deltaTime);
    19.                 SetOrKeepState(State.WalkingUp);
    20.         }
    21.         //Down
    22.         else if (vertInput < 0)
    23.         {
    24.                 transform.Translate(Vector3.down * walkSpeed * Time.deltaTime);
    25.                 SetOrKeepState(State.WalkingDown);
    26.         }
    27.     }

    thanks in advance! :)
     
  2. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    What is the problem -- that you're getting diagonal movement or that two keys pressed at once don't act the way you think they should? Pressing two keys at once isn't something you want the player to do, is it?

    Jay
     
  3. Sedamnest

    Sedamnest

    Joined:
    Aug 3, 2016
    Posts:
    3
    yeah, the behaviour isnt what it should be..2 Buttons pressed at once, should result in reacting to the latest button pressed. only left, right, up an down :)

    what i want:
    - no diagonal movement (works with Code above)
    - 2 buttonpresses allowed. always move in the direction of the latest button pressed.
    For example:
    player presses the left arrow key and moves left. while the left key is still being held down, the Player presses the down key. Then I want him to move to the lateSt directional key pressed, downwards in this case.

    with the Code above, this switch only works, if the first key pressed was for vertical movement. but if i hold down a horizontal key (left for example) and Press a vertical key afterwards, he ignores the second button input and continues moving horizontically :/

    hope i can clarify myself enough
    thanks :)
     
  4. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    My first thought is, would that ability be keyboard-specific? I have no idea, but it might be something to think about.

    From a code perspective, if two keys are pressed can you tell? If so, then when a key is pressed set a flag (such as downPressed = true;) and when two keys are pressed at once, don't call the move code on the one that's already been pressed (and clear that flag for next time; or when that already pressed key is up).

    Jay
     
  5. SystemId

    SystemId

    Joined:
    Aug 5, 2016
    Posts:
    11
    I'm really new to Unity in general myself, but I've seen that kind of movement before in the Unity Tutorials. 2D Roguelike "Scavangers". Although that particular game broke the maps down into a grid like system, and your movement was forced onto the grid... but I specifically remember having to write code dealing with non diagonal movement. Might not be any help, but maybe worth looking into.
     
    JayJennings likes this.
  6. Sedamnest

    Sedamnest

    Joined:
    Aug 3, 2016
    Posts:
    3
    thank you guys, you gave me some new ideas to try
    will experiment a little bit (have something in mind already) and post again with my results.