- Home /
Rotate a parent object
So have four wheels on a car and the car body as separate objects that are grouped in the parent object car in my Hiearchy.
I need to access each wheel and the body separately so that I can have the player paint it.
The problem is that when I call transform.parent and try to rotate, the car rotates about a large radius and not about its center.
public Renderer carBody;
public Renderer carWheel1;
public Renderer carWheel2;
public Renderer carWheel3;
public Renderer carWheel4;
void Start ()
{
carBody.material.color = Color.red;
carWheel1.material.color = Color.black;
carWheel2.material.color = Color.black;
carWheel3.material.color = Color.black;
carWheel4.material.color = Color.black;
}
// Update is called once per frame
void Update ()
{
var rotationDirection = new Vector3(0, 1, 0);
var carObject = carBody.transform.parent;
carObject.transform.Rotate((rotationDirection * Time.deltaTime * rotationSpeed));
}
I need to know how to rotate the parent around its center and not around a larger radius of rotation.
Thank you in advance.
Answer by robertbu · Feb 15, 2013 at 09:30 PM
What is the parent of the carBody? Typically for rotation I will make the parent an empty game object and place it at the center of the car (i.e. the point I want to rotate around). If the parent needs to be off center, you can add an empty game object in between.
That was perfect. I had created an empty game object but I had it in a different place from the rest of the car. Thanks man!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do you manually override mecanim's interpolation with rotations? 2 Answers
Rotate an object around z axis on user input 1 Answer
RotateAround Limitations 2 Answers