- Home /
GUI controlling other game objects
Hello all! I'm still learning script and would appreciate some help. I am trying to code a Pop-Up GUI that would then have a "Move" button to allow movement on the X and Y axis, and a "Rotate" button that would allow an object to be rotated around its local (0,0,0).
Currently my code looks something like this ..
//defines cube public var isCube : boolean = false; //defines camera public var isCamera : boolean = false; //defines rotation axis public var xAxisRotate : float = 2.0; public var yAxisRotate : float = 2.0; //defines translate axis public var yAxisMove : float = 2.0; public var xAxisMove : float = 2.0; //defines transform selection var transformRotate : boolean = false; var transformMove : boolean = false; //is GUI shown private var guiShown : boolean = false;
// GUI function OnGUI() {
//Only run GUI on camera
if(isCamera){
//If toggle button is hit
if(GUI.Button (Rect(Screen.width - 50,10,40,20), "<->")){
//Toggle showing the GUI
guiShown = !guiShown; } if(guiShown){
//GUI shown
//rotate and move
if(GUI.Button (Rect(10,10,200,20), "Rotate Object")){
transformRotation();
}
if(GUI.Button (Rect(10,30,200,20), "Move Object")){
transformMovement();
}
} }
//if rotation is selected function transformRotation(){ transformRotate = !transformRotate; if(transformRotate){ transformMove = false; Debug.Log("Rotate Toggled"); } } //if move is selected function transformMovement(){ transformMove = !transformMove; if(transformMove){ transformRotate = false; Debug.Log("Move Toggled"); } }
//if mouse is dragged function OnMouseDrag(){ //and rotate is selected , object that is cube will.. if(isCube && transformRotate && !transformMove){
var v : float = xAxisRotate * Input.GetAxis ("Mouse Y");
transform.Rotate ( v, 0, 0, Space.World);
var h : float = yAxisRotate * Input.GetAxis ("Mouse X");
transform.Rotate (0, -h, 0,Space.World);
}
//and move is selected , object that is cube will..
if(isCube && transformMove && !transformRotate){
var y : float = xAxisMove * Input.GetAxis ("Mouse Y");
Camera.main.transform.position -= Vector3(0,y,0);
var x : float = yAxisMove * Input.GetAxis ("Mouse X");
Camera.main.transform.position -= Vector3(x,0,0);
}
}
Sorry if it is not properly formatted, im still learning how to post on Unity Answers. The GUI pop-up works and the buttons toggle correctly, but I can not rotate or transform the "isCube" object. I believe this is because the command is inside "isCamera", but I cant think of a work around for this.
Thanks for taking a look at my code!
-Garrett