Rotate toward the target using Rigidbody2D MoveRotation

Hello there,
I’m working on a 2D strategy game. So far I’m moving and rotating objects using Transform. And I recently realized I was wrong, thanks to @MelvMay . Here is the script I was using to rotate objects towards the target:

Vector2 vectorToTarget = targ - pos;
        float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
        Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotateSpeed);

Please help me get same result using Rigidbody2D MoveRotation (Move toward target using moverotation)

Thanks

Soory my bad. Maybe I should post this question in the script subforum. Please move it.

I don’t understand the question. MoveRotation accepts you passing in a Quaternion as the docs show here: Unity - Scripting API: Rigidbody2D.MoveRotation

1 Like

I was using the code above when I am using transform for movement and rotation. The code is working perfectly fine and I just want a equivalent code using Rigidbody.MoveRotation. I understand you might expect me to learn it myself. But please help me on this one. Thanks.

Line 4, you create a Quaternion. Just pass that into the MoveRotation This is what my previous reply was saying that you can pass in your Quaternion to it.

I’m not expecting you to learn anything. I’m not sure how else to help you.

1 Like

You already helped a lot. Thank you so much.