C# Rotate object as if it were a child of that object

Hello guys,

I wanted to know how to rotate an object as if it were childed to that object, I’ve searched around and haven’t found any solution similar or even close to the effect I am trying to get, since me making that object become the child of it messes up the whole object. I’ve tried things like RotateAround but that doesn’t seem to be working particularly well, the effect is not the same.

Thank You

3 Answers

3

Create an empty gameobject at the exact position and rotation of the object you want to rotate around. Parent the object you want to rotate around the first object to that empty and then get the empty to copy the first objects rotation in Update(). There is probably a way to do it with math but I’m afraid I wouldn’t be able to work that out in a hurry :stuck_out_tongue:

I’d love to know your reasoning behind not just parenting it to that object as I think there is most likely a much more simple solution.

Thank you, and I couldn't parent it because I only wanted to parent one part of the mesh and me doing that destroys the mesh so i needed another way to do it instead.

–

You can use RotateAround

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform _parent;

    void Update()
    {
        transform.RotateAround(_parent.position, _parent.rotation.eulerAngles, 20 * Time.deltaTime);
    }
}

Assume object you want to rotate is X, it’s parent Y.
Create an empty gameobject with uniform scale say W, place it at the same position of parent(Y), now make object you want to rotate(X) child of new object (W).

Now assign rotation of Y to W in update().

I think this will solve scaling problem of child.