- Home /
How can i rotate a object more than once (90 degrees)?
Hey, maybe someone can help. I want two separate Sprite-Objects to rotate after clicking +90 degrees. So that they rotate first from 0 to 90, than from 90 to 180, than from 180 to 270 and finally from 270 to 360/0. But not the Sprite-Object itself should rotate. They should linked to an Layer-Object, that rotates. All of them, Layer-Object and Sprite-Objects, should rotate together and not on each own pivot point. So if i click on one Sprite-Object it becomes a child of the Layer-Object. After i clicked on the other Sprite-Object it also becomes a child of the Layer-Object, too.
Code for the Sprites:
public GameObject Layer1;
public GameObject Layer2;
public GameObject Brick;
void Start () {
}
// if you click the Brick it becoming child of Layer
void OnMouseDown (){
if (Brick){
Brick.transform.parent = Layer1.transform;
}
print ("Brick is clicked");
}
Ok, now they are both clicked and linked to the Layer. In the next step i check the numbers of children in the Layer-Object. If the Layer-Object has two children, it rotates from 0 to 90. That works. After this step, i want all children to be unlinked and send back to the normal Brick-Folder, where they belong. So that the Layer-Object is empty again. That works, too. But the next step doesn't work: I want to click again on each Sprite-Object, so that the rotation repeats. Finally it should be as follows: i want to click on two Sprite-Objects. They become children of the Layer-Object. The Layer-Object rotates with both children inside. After the rotation from 0 to 90 is completed, the Layer-Object should become "empty" again. This should be round one! If i repeat the click on the two Sprite-Objects, i want the next rotation of the Layer-Object from 90 to 180. And so on and on... Maybe someone can help.
Thanks and best Greetings. Simon
Code for the Layer:
public GameObject Bricks;
float t;
Quaternion initial = Quaternion.Euler (0,0,0) ;
Quaternion target = Quaternion.Euler (0,0,90) ;
void Start () {
}
void Update () {
print ("Number of Childs: "+transform.childCount);
Rotate ();
Unchild ();
}
public void Rotate () {
// if the Layer has two Children -> rotate
if (transform.childCount == 2) {
t += Time.deltaTime;
transform.rotation = Quaternion.Lerp(initial, target, t); // <---------- how can i code that dynamic? that it goes automatic +90?
print ("rotate");
}
}
public void Unchild () {
// child should go in Bricks, after rotation
if (this.transform.rotation.eulerAngles.z == 90) { // <---------- i want here something like, if the new rotation is done
foreach (Transform child in transform) {
child.transform.parent = Bricks.transform;
print ("Child is back in Bricks");
}
}
}
Answer by Kishotta · Jun 17, 2017 at 05:16 PM
Rather than do all this parenting and unparenting, why not use RotateAround?
Your answer
Follow this Question
Related Questions
How can i rotate a object more than once (90 degrees)? 0 Answers
Flip over an object (smooth transition) 3 Answers
Object Local Y-Up being forced to be World Y-Up 1 Answer
How do i get this objects y rotation the same as my cameras y rotation? 1 Answer
How to Change rotation while preserving local horizontal rotation 1 Answer