- Home /
Rotate object via script like in editor
Hello, I currently have trouble rotating object(with all parented objects). I want same rotation like in editor, just using script. I want to set rotation on y=90, but when I do the object position change, but not in the inspector(just visual). And that's my problem, so I just want to change rotation, and object should stay in place.
Like this: http://videobam.com/VgKrn As you can see when I rotate in editor the rotation and position change. But via script just rotation change, moving object away from its original position.
Note: The object I want to rotate, I created using script, and then I attached some cubes as parent.
-Thanks
There's not enough information here. If you create the object in code, where do you define the pivot point? Are you trying to rotate around the center of the individual object or around the center of the collection of objects? Or rotating the parent?
When I create that object via script and set parent. The object position is where it should be(in center of parented objects). But object position is(0,0,0), and I can see that the object position is not (0,0,0). Then when I rotate, I think it rotates around position(0,0,0). I'm trying to rotate around the center of the collection of objects. Also I never define object pivot.I just create gamobject,then create few cubes, then parent them to gameobject.
Answer by robertbu · May 25, 2014 at 06:27 AM
First, the object position reported in the Inspector is the local position. It is relative to the parent. So you will only get a position of (0,0,0), if the object is in the same position as the parent.
There are a few things I'm still fuzzy about in your questions. Here is a guess at some sample code. It uses a Bounds to calculate the center point of all the game objects based on the pivot point of all the objects in the collection. It will work well if all the objects are approximately the same size and there are no empty game objects in the collection. If you have objects of significantly different sizes, or if you have empty game object in your parented/childed set of objects, then you will need to do some additional work.
Start with a new scene. Build a collection of objects sharing parent/child relationships. Put this script on any object in the collection.
#pragma strict
var pivot : Vector3;
var speed : float = 45.0;
function Start() {
var bounds : Bounds;
bounds.center = transform.position;
var transforms = transform.root.GetComponentsInChildren(Transform);
for (var trans : Transform in transforms) {
bounds.Encapsulate(trans.position);
}
pivot = bounds.center;
}
function Update() {
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.root.RotateAround(pivot, Vector3.up, Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.RightArrow)) {
transform.root.RotateAround(pivot, Vector3.down, Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.UpArrow)) {
transform.root.RotateAround(pivot, Vector3.right, Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.DownArrow)) {
transform.root.RotateAround(pivot, Vector3.left, Time.deltaTime * speed);
}
}