# how to rotate object with rb?

Hi there! I have some issues with writing this code. So basicly it should get the angulear rotation (y) of the rigidbody.

And this object should rotate as the rigidbody does. (only on the y axis) but there should be limits, e.g. if the rigid body rotates more than 20 degrees, the object should not rotate further. Somehow I can’t think of it together.

Here’s my try:

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

public class AutoYaw : MonoBehaviour
{
Rigidbody m_Rigidbody;
Vector3 m_EulerAngleVelocity;

void Start()
{

m_Rigidbody = GetComponent<Rigidbody>();

//rotating around the Y axis, just like the rigidbody
m_EulerAngleVelocity = new Vector3(0, 100, 0);
}

void FixedUpdate()
{
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.fixedDeltaTime);
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
}
}
``````

More than 20 degrees from what? Where it started?

I imagine you would use Quaternion.Angle() and clamp it back when it exceeds what you want.

i meant that if the rigidbody rotates 2 degrees, the script should make another gameobject also rotate 2 degrees.
that until the rigidbody rotates more than let’s say 30 degrees. the gameobject should not rotate further. So basicly what i mean is that the object rotates with the rigidbody, but not more than 30 degrees (example)

I hope the image makes it more understandable.

I’m pretty sure you can’t rotate a RigidBody without rotating the GameObject. You could have a Parent object which stays still and have the Child rotate though.

i want to read the angle of the rigidbody and set the angle to the object…
but the object should not rotate more than 20°

What I’m saying is there’s no point, the GameObject will always have the same rotation as the RigidBody. Just read the world-angle of the Child object in my example, and set the angle of the Parent.

I tried but I didnt get it to work. how can I read the rigidbody rotation and rotate the object… It spins in 1 direction endless.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoYaw : MonoBehaviour
{
Rigidbody m_Rigidbody;
Vector3 m_EulerAngleVelocity;
void Start()
{

m_Rigidbody = GetComponent<Rigidbody>();
//rotating around the Y axis, just like the rigidbody
m_EulerAngleVelocity = new Vector3(0, 100, 0);
}
void FixedUpdate()
{
Quaternion deltaRotation = Quaternion.Euler(m_EulerAngleVelocity * Time.fixedDeltaTime);
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
}
}
``````