Search Unity

Diagonal movement doesnt work

Discussion in 'Scripting' started by PvpMiky, Jul 16, 2019.

  1. PvpMiky

    PvpMiky

    Joined:
    Nov 7, 2016
    Posts:
    9
    Hello there,

    I have a question, this script is not mine. and i dont know where the problem is because in the animations it looks good. but when i launch the game it doesnt move diagonal.

    Iam using a 2d sprite with only left and right animations. so when i use W-D my char needs to move to up right and when i use S-D my char needs to move down right. and when i use W-A my char needs to move up left and when i use S-A my char needs to move down to left.


    But some strange things happen. look the video is this a wrong code or is there something wrong with the sprite sheet.

    Video:


    Code:
    Code (CSharp):
    1.     // simple WSAD movement without prediction
    2.     Vector2 lastDirection;
    3.     [Client]
    4.     void WSADHandling()
    5.     {
    6.         // don't move if currently typing in an input
    7.         // we check this after checking h and v to save computations
    8.         if (!UIUtils.AnyInputActive())
    9.         {
    10.             // get horizontal and vertical input
    11.             // note: no != 0 check because it's 0 when we stop moving rapidly
    12.             float horizontal = Input.GetAxis("Horizontal");
    13.             float vertical = Input.GetAxis("Vertical");
    14.             float diagonal = Input.GetAxis("Horizontal, Vertical");
    15.  
    16.             // create direction, normalize in case of diagonal movement
    17.             Vector2 direction = new Vector2(horizontal, vertical);
    18.             if (direction.magnitude > 1) direction = direction.normalized;
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Line 14 is wrong. It is looking for an input called "Horizontal, Vertical", not merging the two.

    It would have to be something like:
    Code (csharp):
    1. float diagonal = horizontal + vertical;
    Ultimately, it shouldn't matter for the animations. You need to make sure the Animator controller graph has a variable for storing the Horizontal/Vertical input. Then you need to send it the values, for example:
    Code (csharp):
    1. animator.SetFloat("Horizontal", horizontal);
    2. animator.SetFloat("Vertical", vertical);
    It should animate your character automatically from this information.
     
  3. PvpMiky

    PvpMiky

    Joined:
    Nov 7, 2016
    Posts:
    9
    Thanks for your reply, i checked the code i used and the animator part is in it:
    Code (CSharp):
    1. animator.SetFloat("LookX", lookDirection.x);
    2. animator.SetFloat("LookY", lookDirection.y);
    I changed line 14. but it is still not moving correct. maybe there is something wrong with my animator controller?
    But the weird thing is, if you check the video when i control the character and i use S-D to go down right its only reacting to my down key or right key, you can see it in the video the red dot is not reacting to the S-D key at the same time.

    Screenshot (9).png Screenshot (10).png
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    It works when click-dragging the red dot, right? So, the problem is with the data you are sending to the animator.

    lookDirection is a directional vector, so I don't think sending the x/y will help. Try this to test:
    Code (csharp):
    1. animator.SetFloat("LookX", Input.GetAxis("Horizontal"));
    2. animator.SetFloat("LookY", Input.GetAxis("Vertical"));
    Also, check out lookDirection.magnitude and lookDirection.normalize if you want to try to use the direction.
     
  5. PvpMiky

    PvpMiky

    Joined:
    Nov 7, 2016
    Posts:
    9
    Stardog, you are the best. this solution worked for me.
    How can i thank you, really glad that you reacted to my potst and again thanks for your help.

    I owe you one really.