# Rotate Rigidbody2d slowly for 360 degrees

I would like to perform a 360 degree rotation on a GameObject with rigidbody2d component. You should be able to see the rotation, so I thought of rotating the object 8 times with 45 degrees each around the z axis. So far, one might think that this should be an easy task. But, after several hours, I'm really stuck here.

I tried several code snippets (although not understanding them completely, I must admit):

void FixedUpdate()
{
rigidbodyComponent.MoveRotation(45.0f);
}


==> Rotates the object one time for the desired angle.

void FixedUpdate()
{
rigidbodyComponent.MoveRotation(45.0f);
}


==> Nothing happens.

Could you please give my a hint in what direction (meaning: classes / methods) I should proceed?

Thanks.

Rigidbody2D.MoveRotation documentation

Rotates the rigidbody to angle (given in degrees).

In short, you are setting the orientation of the rigidbody to be 45 degrees over and over again. To rotate it over time, you would need to set its rotation to different values (rather than just a constant 45). Here's a simple example using a coroutine to do what you want, in 8 steps, each step .5 seconds apart.

void Start()
{
StartCoroutine(DoRotate());
}

IEnumerator DoRotate() {
for (int i = 0; i < 8; ++i) {
rigidbodyComponent.MoveRotation(45 * i);
yield return new WaitForSeconds(.5f);
}
}

• thank you, your solution works like a charm. One thing I'd like to add: always check in editor that 'Freeze Rotation Z' is disabled ;). Commented Nov 16, 2017 at 20:17
• One thing I forgot to mention: Instead of 'IEnumerable DoRotate() ...', it should be 'IEnumerator DoRotate() ...'. Commented Nov 17, 2017 at 8:04
• Ah, yes. I can never remember which. Commented Nov 17, 2017 at 15:07

However I think I have the solution.

1. Go to your game object you want to rotate
3. Type Rotate This Object
• A this script will allow you to rotate the object on any of its 3 axis.
4. Enter 360 in the Z or Y axis, which ever you'd like.
5. Play the scene and you should see it rotate endlessly.
• If the object drops through the play scene try disabling the "Use Gravity checkbox in the Rigidbody component of the game object.

If you do not have this script automatically in the list of available scripts you may be using an earlier version of Unity under 2017.1. I'd suggest upgrading. Let me know if this helps!

Edit: I forgot you wanted 2D rotation

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class RotateThisObject : MonoBehaviour
{
public float z_Rotation;
public float rot_Speed = 1;

void Update()
{
transform.Rotate(new Vector3(0,0,
(z_Rotation > 0) ? z_Rotation * Time.deltaTime * rot_Speed
: 0));
}
}


That should work perfectly. I modified the script that came with unity called RotateThisObject. I suggest just copying the code into your own script instead of overwriting RotateThisObject like I did.

• Note that it's perfectly OK to ask Unity questions here rather than on Unity Answers. As a user of both sites, I personally prefer the features we have here on GameDev.StackExchange, I find the questions/answers/discussion better-curated/edited/tagged here (and I've only been a mod for a week so I'm not claiming any credit for that!), and I generally find more complete, validated answers here. Other users' preferences may vary, of course. Commented Nov 16, 2017 at 17:26
• I've implemented your code into my update method, but nothing happens. Unfortunately, there's no rotation at all. I also thought of using ridigbody2d.transform.Rotate(...), but it didn't work. Commented Nov 16, 2017 at 20:31

Like you said you already have a rigidbody2d attached, so take a look at the following link

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public float speed = 45;
public Rigidbody2D rb2D;
void Start() {
rb2D = GetComponent<Rigidbody2D>(); //Get component attached to gameobject
}
void FixedUpdate() {
rb2D.MoveRotation(rb2D.rotation + speed * Time.fixedDeltaTime);
}
}


Now I am not a 100% certain but if you set the speed too, lets say 45 it should adjust it by 45 degree angle every second. I suggest reading all the info given in the link to get a better idea

• It works, but the rotation is very slow. I set the speed to 1000, even with this it take a while. Commented Nov 16, 2017 at 20:28