- Home /
A very stupid rotate question
Hi all, I have an empty gameobject which is a parent to other objects. I can move the empty parent and all children move nicely with it. No problem. However its a different story when I try and rotate it. When I use transform.rotate() the object does indeed rotate, but around the pivot point and so the object moves out of position. I read that all rotations done in scripts(Im using C#) rotate around the pivot point, not the centre point. How can I tackle this? I just want to rotate the parent in one axis around its centre. Thanks
Answer by tanoshimi · Apr 03, 2015 at 08:28 AM
Use Transform.Rotate...?
When I use that the object rotates around the pivot not the center(as stated in the op)and as a result the object moves out of position.
You also said it's an empty gameobject, so you can position it wherever you want (and reposition its children relative to it as appropriate), and all the children will rotate around that point. An empty gameobject is represented by a singular transform and has no visible mesh, so there is no sense of a "center" or "pivot" - it's just a singular position in space.
If that's not what you want then you might care to add a diagram to your question, because at the moment its unclear what the problem is.
ah cool. I have a set of cubes arranged to form a loop. The loop can be n-sided, all sides contain the same number of cubes.I then parent these cubes to the empty game object. I then try and rotate the empty game object and the loop looks like it moves around a non centre point. Yet if i select the empty gameobject in the editor and use the rotate widget it rotates about its centre just fine.
Create an empty gameobject in your scene and add this script - it should create n cubes in a regular shape around the parent (empty) gameobject as you describe, and then rotates that object using Transform.Rotate.
You should see all the cubes rotate around the centre point?
using UnityEngine;
using System.Collections;
public class TestRotate : $$anonymous$$onoBehaviour {
private int numCubes = 20;
private float radius = 5f;
private float rotationSpeed = 5f;
private Transform _trans;
// Use this for initialization
void Start () {
_trans = GetComponent<Transform>();
for(int i=0; i<numCubes; i++){
float b = $$anonymous$$athf.PI * 2f / numCubes * i;
GameObject temp = GameObject.CreatePrimitive(PrimitiveType.Cube);
temp.transform.position = new Vector3(radius * $$anonymous$$athf.Sin(b), 0, radius * $$anonymous$$athf.Cos(b));
temp.transform.parent = this.transform;
}
}
// Update is called once per frame
void Update () {
_trans.Rotate(new Vector3(0, rotationSpeed * Time.deltaTime, 0));
}
}
Your answer
Follow this Question
Related Questions
How to make a cube turn in the direction it is moving? 1 Answer
How can I rotate an object without moving it up or down? 0 Answers
Determine if the game object has completely rotated once around its centre. 1 Answer
Positions to rotations of a regular GO 2 Answers
Build rotation tool for level editor 0 Answers