I can not configure an adequate change in the character's transform.rotation using a virtual joystick.
To the left and to the right, the character turns like normal - in the screenshot I have shown green arrows.
Up, down and diagonally the character turns wrong - in the screenshot I displayed red arrows
enter image description here
Video how it all works at the moment - https://youtu.be/2NWMwxXn2mc
Explanation for the video:

  • From 1 to 6 seconds, I show how the character turns horizontally (right-left) correctly
  • From 6 to 24 seconds, I show the character turning incorrectly diagonally and up and down.

How it should work: I pull the joystick up - the character turns up, I pull the joystick down - the character turns down, the same for all sides, including turns diagonally In math, unfortunately, to put it mildly, not strong Script:

 public Joystick joystick; float tiltAngle = 180.0f; float smooth = 5.0f; void Update () { Quaternion target = Quaternion.Euler(0, joystick.Vertical * tiltAngle, 0); transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); Quaternion target2 = Quaternion.Euler(0, joystick.Horizontal * tiltAngle, 0); transform.rotation = Quaternion.Slerp(transform.rotation, target2, Time.deltaTime * smooth); } 

    2 answers 2

    1. First you need to determine the management comes from the character or from the camera. This is not written. That is, absolute turns or relative.
    2. I suppose that the character must respond to the camera.

    3. It is necessary to calculate the current direction of the camera.

    4. It is necessary to carry out manipulations to translate the absolute angles of rotation (world coordinate system) to the angles of rotation of the character (coordinate system relative to the camera). Most likely, this is solved by adding or subtracting vectors including only a couple of vector values. Xs which - figure it out yourself.
    5. And then tie the joystick to the rotation of the character. That is, to bring the screen coordinate system (x, y) - the position of the joystick in the coordinate system relative to the camera. You turn the joystick to the left - turns to the left and runs. Turn right - identical.

    And the code that you now will stop working as soon as you change the direction of the camera, most likely)

    And here is another useful: How to make control, like in the game "Overcooked"?

      Solved the problem and also added movement with Velocity!

        if (joystick.Vertical != 0 && joystick.Horizontal != 0) { transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(new Vector3(joystick.Horizontal, 0f, joystick.Vertical)), speed * Time.deltaTime); rb.velocity = new Vector3(joystick.Horizontal * movementSpeed, rb.velocity.y, joystick.Vertical * movementSpeed); }