How to rotate an object without rotating its child objects?

Hello, I would like to know how I can rotate an object through a script without rotating its child objects.

Just create a public Transform for your child object and then put something like this in void Start:

childObject.parent = null;

Then you can put in your rotation code and the child object won’t move with it

  • You can change your hierarchy like that and instead of moving and rotating your actual parent, you move the object “NewParent” and rotate the object “ActualParent”

Capture

  • You can also make an object act as a child without being one and tell him to follow the parent object without replicating the rotation. But it has to be in Update()
  • You can tell the childs to rotate the opposite of the parent when the parent rotate
  • You can unchilds your childs when the parent rotates and move them back to child just after. But it will stop to follow the position of the parent during the process.

To rotate an object without rotating its child objects through a script, you need to use the transform component of the GameObject. Unity’s transform component represents an object’s position, rotation, and scale in the 3D space. To achieve this, you can use the following C# script:

public class RotateObject : MonoBehaviour
{
       // Set the rotation speed in degrees per second
    public float rotationSpeed = 30f;
   
    void Update()
    {
     // Rotate the object around its own Y-axis (upward direction)
        transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
    }
}

Here’s how to use the script:

  1. Attach this script to the GameObject you want to rotate.
  2. Set the rotationSpeed variable to the desired speed of rotation.

This script rotates the object around its own Y-axis, which means it spins around itself without affecting its child objects. The Time.deltaTime ensures that the rotation is frame-rate independent.

Remember to adjust the rotationSpeed according to your preference. You can attach this script to any GameObject you want to rotate without affecting its children.