Let me say exactly what am I trying to do. I’ve got for cubes (which in turn combines to form a square) and when I press a button all the four should rotate on a common pivot point (the center of the square). OK? and here goes my code for that:-
var pointOne=new Vector3(-0.5,-0.5,0);
var axisPoint=new Vector3(0,0,-20);
var totalRotation:float=0;
var isRotating:boolean=false;
function OnGUI()
{
if (GUI.Button(Rect(10,70,50,30),"Click"))
{
isRotating=true;
}
if(isRotating)
{
Rolling();
}
if(totalRotation>90)
isRotating=false;
Debug.Log("Clicked");
}
function Rolling()
{
var spinAmount:float=20*Time.deltaTime;
if(totalRotation<90)
{
transform.RotateAround (pointOne, axisPoint, spinAmount);
}
totalRotation+=spinAmount;
Debug.Log(totalRotation);
}
When I tried this, only one cube rotates as desired (bottom right), while this code is attached all the cubes! Then I commented the whole “function OnGUI()” and renamed the “function Rolling()” to “function Update()” …, all cubes rotates as desired! Can any one guide me, Please???
You are creating 4 buttons on the same place, one over the other, but only the one at the top is pressed.
You should create one separated script to handle the GUI and pass the event to the cubes.
cubeRotation.js (this code is attached to four different colored cubes and I intend them to be evoked when the GUI button is pressed. Here goes the code:-
public var totalRotation:float=0;
function Rolling()
{
var pointOne=new Vector3(-0.5,-0.5,0);
var axisPoint=new Vector3(0,0,-20);
var spinAmount:float=20*Time.deltaTime;
if(totalRotation<90)
{
Debug.Log(gameObject);
transform.RotateAround (pointOne, axisPoint, spinAmount);
}
totalRotation+=spinAmount;
//Debug.Log(totalRotation);
}
here goes the script for button(forButton.js):-
var cuberotation:cubeRotation;
cuberotation=FindObjectOfType(cubeRotation);
var isRotating:boolean=false;
function OnGUI()
{
if (GUI.Button(Rect(10,70,50,30),"Click"))
{
//Debug.Log("Clicked");
isRotating=true;
}
if(isRotating)
{
cuberotation.Rolling();
// Debug.Log("Rolling called");
// Debug.Log(gameObject);
}
// if(cuberotation.totalRotation>90)
// isRotating=false;
//
}
So what I did:-
I created an empty game object and attached a button to it. Then I attached just above(forButton.js) code to the button. Then when I pressed play, only one of the cube is rotating. The rotating cube happens to be the one I attached the code last. ie, if I attach the code to a red cube then red will rotate. Then I attached that code to green then red won’t rotate and green will rotate. Then again when I attached the code to Blue then red and green won’t rotate only Blue rotates!!!