- Home /
Rotating game Object.
I know this questions have been asked multiple times and many solutions have been given.And some are working according to my requirements though in different example,but not in the actual project that i am working on. The problem is i want to rotate a gameobject around its center. The game Object here is not a simple one though. It may look something like ![this ][1] or like ![this][2] or may be some other form . All the component you see are different objects and as and when they are coming on the screen they are made child of this gameobject. So in the first case main game Object is named as singleLine. So all the components on screen are made child of singleLine. Now the position of singleLine would be the center of E-2. I am not been able to rotate it around the exact center of E-2 rather it moves in a circular manner. How to do this. I am posting a script out here of all the things that i have tried.
var targetItem : GameObject;
var rotationRate : float = 1.0;
var hit: RaycastHit;
public var background:GameObject;
public var cam:Camera;
var smooth = 1.0;
var tiltAngle = 30.0;
function Update()
{
if (Input.touchCount > 0)
{
var theTouch : Touch = Input.GetTouch(0);
var ray = cam.ScreenPointToRay(theTouch.position);
if(Physics.Raycast(ray,hit))
{
if(hit.collider==background.collider)
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Moved)
{
//this code is for x-axis rotation
if(Input.GetTouch(0).deltaPosition.y < 0|| Input.GetTouch(0).deltaPosition.y > 0)
{
var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
var target = Quaternion.Euler (tiltAroundX, 0, 0);
targetItem.transform.rotation = Quaternion.Slerp(targetItem.transform.rotation, target,Time.deltaTime * smooth);targetItem.transform.Rotate(0,theTouch.deltaPosition.x * rotationRate,0,Space.World);
targetItem.transform.Rotate(theTouch.deltaPosition.x * rotationRate,0,0,Space.World);
}
if(Input.GetTouch(0).deltaPosition.x < 0 || Input.GetTouch(0).deltaPosition.x > 0)
{
var tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
var target1 = Quaternion.Euler (tiltAroundY, 0, 0);
targetItem.transform.rotation = Quaternion.Slerp(targetItem.transform.rotation, target1,Time.deltaTime * smooth);
targetItem.transform.Rotate(0,theTouch.deltaPosition.y * rotationRate,0,Space.World);
}
wasRotating = true;
}
}
}
}
}
}
Have tried using only targetItem.transform.Rotate(0,theTouch.deltaPosition.y * rotationRate,0,Space.World);
as well but again not helping out . Tried targetItem.transform.rotation(targetItem.transform.position,Vector3.down,20*Time.maximumDeltaTime);
as well but again similar result.What could be the reason for it. Please help me. [1]: /storage/temp/16892-firstlayout.png [2]: /storage/temp/16893-secondlayout.png
Answer by Tomer-Barkan · Oct 23, 2013 at 08:15 AM
Make an empty GameObject
, call it center
, and locate in the exact center of the object (the exact point that you want the rest of the components to rotate around).
Then make all the other components children of the center
game object.
In your code, add a reference to the center
object, and then it's very simple to rotate everything around it:
center.transform.Rotate(xAngle, yAngle, zAngle);
well that's what i am doing. Created an empty GameObject 'center' by using transform.parent
and made all the other element its child. The problem i see is that as the gameobjects increases the 'center' get shifts to the center of the entire game Object.I would say the same script is working as gem in test demo. In which all the game objects are created in editor and not at run time.
I didn't see any object creation in your code, I thought you created them in the editor... share that part of the code please. If you do create them in the editor, share the object hierarchy.
`cylinderbond.transform.parent=GameObject.Find("RotateElementCenter").transform;`
and this is code for creating the connection between the two spheres sphereElement.transform.parent=GameObject.Find("RotateElementCenter").transform;
for spheres. And for hierarchy its something like this ::
RotateElementCenter 1. Element1 2. Element2 3. Cylinder1 and so on..
The RotateElementCenter
position should not change when you attach new children to it. You can check in the inspector while running your game.
There must be something else going on that's interfering.
I tried to recreate a scenario, had a empte center objet, and two cubes located around it but not attached as children.
Then I had a script that would attach both children to the parent:
public class Test2 : $$anonymous$$onoBehaviour {
void Start () {
transform.parent = GameObject.Find("Center").transform;
}
}
And another one that would rotate the center object:
public class Rotate : $$anonymous$$onoBehaviour {
void Update () {
transform.Rotate(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z + Time.deltaTime * 0.0001f);
}
}
And it works just fine. So it's not a problem with that part of the code, there might be other factors in your game that are affecting this.
$$anonymous$$ay be some other problem I take that . I will check it in the demo i created and see if that is working fine..! will get back to you .. !
Answer by yogee · Oct 23, 2013 at 09:07 AM
for (var i : Transform in singleLine)
{
allPoint+= i.position;
}
var groupCenter = allPoint/ singleLine.childCount;
it,s a vector man, i.e, WholeObjCenter= Sum of all objs position / no. of objs(child)