-1
\$\begingroup\$

My parent object components: Rigidbody2D and the script I shared below.

Children(Head, Body, Arm, Backpack...): There is only collider. There is no rigidbody2D. (I want the parent object to use the children's colliders, so I haven't add a rigidbody)

I want all my objects to rotate smoothly on their own axis. In the script below, it does not rotate on its own axis.

[SerializeField] public DynamicJoystick joystick;
public float speed;
Vector2 move;
public Rigidbody2D rb;

//Smoothly Turn
float _z;
float angle;
float turnSpeed = 15f;

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);

}
\$\endgroup\$
3
  • \$\begingroup\$ Can you show us 1) how your object with the rigidbody is set up in the scene view, with the transform gizmo in Local Pivot mode so we can see where the object's origin is, and 2) what rotation behaviour you're observing and what makes it "not on its own axis"? Also, bear in mind that the delta time correction in your Slerp is wrong. \$\endgroup\$
    – DMGregory
    Sep 18 at 13:52
  • \$\begingroup\$ After I prepared my gameobjem with children, I made prefabs. my gameobject's origin is not unitiy's stage origin (when I reset the transform all objects move to the right of center). While rotating the parent gameobject, instead of rotating on its own axis, it pretends to rotate around a circle. \$\endgroup\$
    – gunza
    Sep 18 at 15:32
  • \$\begingroup\$ Please edit your question to show this setup in a screenshot as requested. \$\endgroup\$
    – DMGregory
    Sep 18 at 15:39
1
\$\begingroup\$

When I gave a gizmo to my parent object, I saw that the gizmo wasn't in the center of my parent object. So the character is turning, but instead of rotating on its own axis, it rotates around the gizmo. I simply moved all the children to the center of my parent object (right in the middle of the gizmo) and the problem was solved.

\$\endgroup\$
0
\$\begingroup\$

Have you tried Rigidbody2D.MoveRotation ?

Example (press any key to rotate the object to 10 degrees):

[DisallowMultipleComponent]
public class Rotate : MonoBehaviour
{
    [SerializeField]
    private float _angleVelocity = 10f;

    private Rigidbody2D _rigidbody;
    private bool _rotate;

    private void Awake()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if (Input.anyKey)
            _rotate = true;
    }

    private void FixedUpdate()
    {
        if (!_rotate)
            return;

        _rigidbody.MoveRotation(_rigidbody.rotation + _angleVelocity * Time.fixedDeltaTime);
        _rotate = false;
    }
}

It is recommended to read the input data using the Update method since it is called every frame.

\$\endgroup\$
2
  • \$\begingroup\$ I tried this, but it didn't work. \$\endgroup\$
    – gunza
    Sep 18 at 15:34
  • \$\begingroup\$ What exactly didn't work? The code works fine, I tested it before posting. Most likely you have a problem somewhere else and not in code. Can you create a new minimal project that reproduces your problem and share it on github or somewhere else? \$\endgroup\$
    – Sergey
    Sep 18 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.