P
P
password222020-11-19 11:08:58
Unity
password22, 2020-11-19 11:08:58

(Unity 3D, character movement) Why is running diagonally slower than running right/left or straight/backward?

Hello. I made the movement of the character in 3d, but it turns out that when the character moves diagonally, and not in a specific direction, then his speed becomes less. I did not find a similar problem on the Internet, I do not understand what's wrong.

I make for mobile, so I use the Input System for the joystick.

private void Start()
    {
        controller = GetComponent<CharacterController>();
        cameraMain = Camera.main.transform;
        anim = GetComponent<Animator>();
        animAimRun = false;
    }

    void Update()
    {
              
        Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>(); // движение джойстика
        Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);

        controller.Move(move * Time.deltaTime * playerSpeed); // x z

        anim.SetFloat("inputX", movementInput.x);
        anim.SetFloat("inputY", movementInput.y);

        if (movementInput != Vector2.zero) // rotation
        {
            float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
        }

    }


Animation made like this:
5fb6273a7febe591012304.png

I would be glad for any help or just a hint, what's the matter.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-11-19
@ueruxon

Try move to normalize.

Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
move = move.normalized;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question