.MoveRotation doesn't seem to do anything at all

Hello,

I am now trying to use .MoveRotation, but it doesn’t work at all.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotate : MonoBehaviour
{
    private Rigidbody2D rb;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void FixedUpdate()
    {
        rb.MoveRotation(rb.rotation + 90 * Time.fixedDeltaTime);
    }
}

I have tried looking up why this is not doing anything, but from what I can find this should be working like this. Am I doing something wrong, am I missing something or is something else going on?

You can not add a float to a quaternion. Check the docu on quaternions :slight_smile:

I’d say look at Unity’s example, your code doesn’t even compile.

your code should be like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotate : MonoBehaviour
{
    private Rigidbody2D rb;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void FixedUpdate()
    {
var rotation = new Vector3(0, 90, 0) // rotating in the y axis
        rb.MoveRotation(rb.rotation + 90 * Time.fixedDeltaTime);
    }
}

your code should be like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotate : MonoBehaviour
{
    private Rigidbody2D rb;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void FixedUpdate()
    {
        var rotation = new Vector3(0, 90, 0) // rotating in the y axis
        rb.MoveRotation(rb.rotation + rotation * Time.fixedDeltaTime);
    }
}

Thanks a lot for your awnsers,

Unfortunately I seem to not be able to make .MoveRotation work.

Kreshi:
I am using a Rigidbody2D component, so it is possible to use a float instead of an quaternion as argument for .MoveRotation. The issue is: even if I make it into a quaternion, it still doesn’t seem to do anything.

Brathnann:
The link you’ve put in your reply is for the Rigidbody component, but I am using a Rigidbody2D component. I am not sure if that matters but here’s the link to that:

From what I know my code is pretty much an exact copy of the example giving by Unity, at least for the rotation.

Also I do not understand what you mean by “your code doesn’t even compile”. I am that good in coding yet, but I would love to learn what you meant with that.

TheDevloper:
I have tried copying the code you made for me, but it gives me te following error:
Assets/Scripts/rotate.cs(14/25): error CS0019: Operator ‘+’ cannot be applied to operands of type ‘float’ and ‘Vector3’
I am pretty sure I get this error because rb.rotation is a float for a Rigidbody2D component, since only the Z-rotation is used. Unfortunately I can’t fix my initial issue by changing “rotation” to a float either.

There’s nothing wrong with this code, other than the fact that you are not following the convention for C# classes to use PascalCase names.

Note many of the commenters above are confused and seem to think you’re using a 3D Rigidbody, which you are not.

Your code is completely fine, I would guess that one or more of the following is the actual problem:

  • You haven’t actually attached this script to a GameObject
  • You have one or more compile errors somewhere in your code that you haven’t addressed
  • You haven’t actually run the game
  • The GameObject you attached this script to doesn’t have a Rigidbody2D component on it
  • The GameObject you attached this script to isn’t active or is not even in the scene.
3 Likes

Yes, odd, for some reason I thought I saw just a regular Rigidbody and not the 2D one.

2 Likes

Hey don’t know if you’re still having issues but I found this thread because I had the same issue. Turned out I had ticked the “Freeze Rotation” box under constraints in the Rigidbody2D component. After unchecking the box it works as predicted.

1 Like

I hope almost a year later they aren’t still stuck on this. :smile:

3 Likes

If you’re using Rigidbody.MoveRotation it’s because you want to directly set the rotation yourself and don’t want external forces messing that up.Unity literally tells you to freeze rotation if you want to use it