Search Unity

HotlineMiami-like controls

Discussion in '2D' started by tarzanno, Apr 23, 2015.

  1. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Hi there!
    This is the second day of my Unity adventure and I already have a problem.
    I want my game to have aerial camera view and player should walk in 4 directions- on axle x and y.
    At the moment, I managed to make it walk in these directions, but there is a problem: when it walks in the x directions (pressing A or D on keyboard), I can't make it move to the y direction at the same time. When I start to walk with W and S on y axle and want to go on the angle, pressing A and D at the same time, it works perfectly. Can you help me?

    This is the source code in JavaScript:

    Code (JavaScript):
    1.  
    2. var MoveRight : KeyCode;        //Zmienne ruchu
    3. var MoveLeft : KeyCode;
    4. var MoveUp : KeyCode;
    5. var MoveDown : KeyCode;
    6. var Shift : KeyCode;
    7.  
    8. var runR : Sprite;                //Sprite ruchu
    9. var runL : Sprite;
    10. var runU : Sprite;
    11. var runD : Sprite;
    12. var Nrun : Sprite;
    13.  
    14. var Speed : float = 1;
    15. var end: Transform;
    16. function OnTriggerEnter(end){
    17. Application.LoadLevel(1);
    18. }
    19.  
    20.  
    21.  
    22.     function Update () {
    23.    
    24.         if (Input.GetKey(MoveRight)){
    25.             GetComponent.<Rigidbody2D>().velocity.x=Speed;
    26.             GetComponent(SpriteRenderer).sprite = runR;
    27.         }
    28.        
    29.          else if (Input.GetKey(MoveLeft)){
    30.             GetComponent.<Rigidbody2D>().velocity.x = Speed*-1;
    31.             GetComponent(SpriteRenderer).sprite = runL;
    32.         }
    33.        
    34.          else if (Input.GetKey(MoveUp)){
    35.             GetComponent.<Rigidbody2D>().velocity.y = Speed;
    36.             GetComponent(SpriteRenderer).sprite = runU;
    37.         }
    38.        
    39.         else if (Input.GetKey(MoveDown)){
    40.             GetComponent.<Rigidbody2D>().velocity.y = Speed*-1;
    41.             GetComponent(SpriteRenderer).sprite = runD;
    42.         }
    43.        
    44.         else {
    45.             GetComponent(SpriteRenderer).sprite = Nrun;     //stanie
    46.             GetComponent.<Rigidbody2D>().velocity.x=0;
    47.             GetComponent.<Rigidbody2D>().velocity.y=0;
    48.             //Speed = 0;
    49.         }
    50.        
    51.        
    52.         if (Input.GetKey(Shift)){
    53.         Speed = 10;
    54.         }
    55.         else{
    56.         Speed = 5;
    57.         }
    58.        
    59.                 }
     
  2. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Also, I'd like to make my character look at the direction which a mouse cursor is pointing at. Is it even possible? :)
     
  3. Kuan

    Kuan

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    87
    Hi!

    Just to answer why you cannot have 2 key down events at the same frame: it is because you're using "else if" for the condition checking. Simply use "if" to check for each 4 keys and manipulate the velocity should be good enough. Better, check out the answer in this link : http://answers.unity3d.com/questions/635447/2d-top-down-movement.html

    One problem though, your sprite assignment will becomes an issue. To accommodate the previous change, you will need another method to determine the sprite based on the final velocity calculated.

    You mentioned Hotline Miami. You might want to check out this Learn video : http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/top-down-2d. This might be the actual thing you need instead of the above method.
     
    LiberLogic969 and tarzanno like this.
  4. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Thank you very much. I combined solutions from both of your links and it is working perfectly fine!