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?
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);
}
}
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);
}
}
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.
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.