Diagonal movement doesnt work

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

Code:

    // simple WSAD movement without prediction
    Vector2 lastDirection;
    [Client]
    void WSADHandling()
    {
        // don't move if currently typing in an input
        // we check this after checking h and v to save computations
        if (!UIUtils.AnyInputActive())
        {
            // get horizontal and vertical input
            // note: no != 0 check because it's 0 when we stop moving rapidly
            float horizontal = Input.GetAxis("Horizontal");
            float vertical = Input.GetAxis("Vertical");
            float diagonal = Input.GetAxis("Horizontal, Vertical");

            // create direction, normalize in case of diagonal movement
            Vector2 direction = new Vector2(horizontal, vertical);
            if (direction.magnitude > 1) direction = direction.normalized;

Line 14 is wrong. It is looking for an input called “Horizontal, Vertical”, not merging the two.

It would have to be something like:

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:

animator.SetFloat("Horizontal", horizontal);
animator.SetFloat("Vertical", vertical);

It should animate your character automatically from this information.

Thanks for your reply, i checked the code i used and the animator part is in it:

animator.SetFloat("LookX", lookDirection.x);
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.

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:

animator.SetFloat("LookX", Input.GetAxis("Horizontal"));
animator.SetFloat("LookY", Input.GetAxis("Vertical"));

Also, check out lookDirection.magnitude and lookDirection.normalize if you want to try to use the direction.

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.