0
\$\begingroup\$

I am using two Joysticks. One for the character's movement and rotation and the other for the character's rotation and shooting. On the joystick I use for movement and rotation, the character turns slowly, so there is no problem. However, the joystick I use to shoot and turn rotates the character very quickly.

Joystick script with no problem:

  void FixedUpdate()
   {
    move.x = joystick.Horizontal;
    move.y = joystick.Vertical;
    float x = joystick.Horizontal;
    float y = joystick.Vertical;
    angle = Mathf.Atan2(y, x) * Mathf.Rad2Deg;


    if (angle != 0)
    {
        _z = angle;
    }

    Quaternion rotate = Quaternion.AngleAxis(_z, Vector3.forward);
    rb.transform.rotation = Quaternion.Slerp(rb.transform.rotation, rotate, turnSpeed * Time.fixedDeltaTime);
    rb.MovePosition(rb.position + move * speed * Time.fixedDeltaTime);

}

Joystick script that rotates the character fast:

    void FixedUpdate()
  {
    if (Mathf.Abs(attakJoystick.Horizontal) > 0.1f || Mathf.Abs(attakJoystick.Vertical) > 0.1f)
    {
        float x = attakJoystick.Horizontal;
        float y = attakJoystick.Vertical;
        float z = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
        rbp.transform.eulerAngles = new Vector3(0, 0, z);

        Quaternion rotate = Quaternion.AngleAxis(z, Vector3.forward);
        gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, rotate, turnSpeed * Time.deltaTime);
    }
}

What do I need to fix in the script, which is the problem above, so that my character turns softly when I use it?

Note:My turnspeed value is 10. There is no problem with the joystick I use for movement and rotation, my character turns slowly. I use the script below for fire and rotation, it does turn, but it is very fast. Children of the canvas on my two joysticks. My character is each other's children as separate objects and the children of a main empty object.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ What s rbp here? It looks like you're rotating two objects: rbp.transform gets snapped instantly to the new angle, while gameObject.transform gets a gradual slerp. \$\endgroup\$
    – DMGregory
    Dec 9, 2021 at 20:53
  • \$\begingroup\$ rbp is Rigidbody2D.. When I replace gameobject.transform with rbp.transform, nothing changes. \$\endgroup\$
    – gunza
    Dec 9, 2021 at 21:15
  • \$\begingroup\$ What I mean is that you should show us your scene hierarchy so we can see how the object with the Rigidbody2D relates to the object with this script. We can't determine that from the snippet you've shown us so far. \$\endgroup\$
    – DMGregory
    Dec 9, 2021 at 22:23

0

You must log in to answer this question.

Browse other questions tagged .