Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question .MoveRotation doesn't seem to do anything at all

Discussion in 'Scripting' started by Slurgelman, Aug 25, 2022.

  1. Slurgelman

    Slurgelman

    Joined:
    Aug 25, 2022
    Posts:
    7
    Hello,

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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rotate : MonoBehaviour
    6. {
    7.     private Rigidbody2D rb;
    8.     void Start()
    9.     {
    10.         rb = GetComponent<Rigidbody2D>();
    11.     }
    12.     void FixedUpdate()
    13.     {
    14.         rb.MoveRotation(rb.rotation + 90 * Time.fixedDeltaTime);
    15.     }
    16. }
    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?
     
  2. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    433
    You can not add a float to a quaternion. Check the docu on quaternions :)
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
  4. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    your code should be like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class rotate : MonoBehaviour
    5. {
    6.     private Rigidbody2D rb;
    7.     void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody2D>();
    10.     }
    11.     void FixedUpdate()
    12.     {
    13. var rotation = new Vector3(0, 90, 0) // rotating in the y axis
    14.         rb.MoveRotation(rb.rotation + 90 * Time.fixedDeltaTime);
    15.     }
    16. }
     
  5. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    your code should be like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class rotate : MonoBehaviour
    5. {
    6.     private Rigidbody2D rb;
    7.     void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody2D>();
    10.     }
    11.     void FixedUpdate()
    12.     {
    13.         var rotation = new Vector3(0, 90, 0) // rotating in the y axis
    14.         rb.MoveRotation(rb.rotation + rotation * Time.fixedDeltaTime);
    15.     }
    16. }
     
  6. Slurgelman

    Slurgelman

    Joined:
    Aug 25, 2022
    Posts:
    7
    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:
    https://docs.unity3d.com/ScriptReference/Rigidbody2D.MoveRotation.html
    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.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
    Last edited: Aug 25, 2022
    Bunny83, angrypenguin and Brathnann like this.
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    Yes, odd, for some reason I thought I saw just a regular Rigidbody and not the 2D one.
     
    Bunny83 and PraetorBlue like this.
  9. jacobearlywine

    jacobearlywine

    Joined:
    Oct 6, 2019
    Posts:
    1
    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.
     
    Bunny83 likes this.
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,148
    I hope almost a year later they aren't still stuck on this. :D
     
    Bunny83 and angrypenguin like this.