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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Need help with Vector2

Discussion in 'Scripting' started by FreeHugsHere, May 2, 2018.

  1. FreeHugsHere

    FreeHugsHere

    Joined:
    Apr 7, 2015
    Posts:
    9
    My level is beginner, so i started from the basic movements. Before to write here, i checked all the possible documentations for this method.
    this is some of working code of my 4 way movement from my player script that explains how it moves

    private void GetInput()
    {

    direction = Vector2.zero;

    if (Input.GetKey (KeyCode.W))
    {
    direction += Vector2.up;
    }
    if (Input.GetKey (KeyCode.A))
    {
    direction += Vector2.left;
    }
    if (Input.GetKey (KeyCode.S))
    {
    direction += Vector2.down;
    }
    if (Input.GetKey (KeyCode.D))
    {
    direction += Vector2.right;

    P.S. up, down,left,right is the shorthands for the coordinates.

    But my next step is to movie object in betweens,

    logically it should be like this?

    if (Input.GetKey (KeyCode.A) && !Input.GetKey (KeyCode.S)) // so the Unity knows that i pressed two button at the same time
    {
    direction += Vector2.left + Vector2.down;

    and this should point at the specific position on the coordinate system


    I have doubts because when i press A(left) the object plays not old(left_animation), but new (left_down_animation), even i didn't press two buttons at the same time. And the right animation won't play either.

    i also use nodes, which contains x&y coordinate parameters & conditions.

    So my question, is it possible to complete my 8way movement by using this method? Any suggestions will be welcomed.

    Thank you!
     
  2. FreeHugsHere

    FreeHugsHere

    Joined:
    Apr 7, 2015
    Posts:
    9
    Fresh idea. Maybe better not to code the inbetween movement, because when i press for example left & down button together, the object moves inbetween anyway?

    So is it better to code only that - when i press two specific buttons, the object will play the right animation?
     
  3. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    You are right, for the movement, you just need to code each key separately but not for the animation.
    Also note that your "inbetween" movements will be faster than the others . You have to normalize your vectors.
     
  4. FreeHugsHere

    FreeHugsHere

    Joined:
    Apr 7, 2015
    Posts:
    9
    im afraid it will looks not how i want it to be. Right now, for example if i move left, when i stop, the system know that it should stay in left idle_image, but if i will animate the inbetween button combinations, the system will not know what to do after i stop press button combinations.

    Any advices on that?

    I also tested this function -

    if (Input.GetKey (KeyCode.S) && !Input.GetKey (KeyCode.A))
    {
    Debug.Log("Pressed.");
    }

    and how i thought, this not worked for the two pressed buttons for a same time, only for the one, in this case - S button.

    Update. !Input.GetKey (KeyCode.A)) - ! was wrong.
     
    Last edited: May 2, 2018