- Home /
Rotate object with a gui texture button
i have an object and 2 gui texture buttons. i want to rotate my object by pressing the 2 buttons (one-left, one-right).
ideas ?
Answer by aldonaletto · Oct 27, 2011 at 01:49 PM
It's simple, use GUI.RepeatButton and transform.Rotate - like this:
var imageLeft: Texture2D; // drag the left button image here var imageRight: Texture2D; // right button image var speed: float = 60; // rotate speed in degrees per second private var rotLeft = false; // object rotates left if true private var rotRight = false; // object rotates right if true;
function OnGUI(){ rotLeft = GUI.RepeatButton(Rect(10,10,200,200), imageLeft); rotRight = GUI.RepeatButton(Rect(230,10,200,200), imageRight); }
function Update(){
if (rotLeft) transform.Rotate(0, -speed*Time.deltaTime, 0);
if (rotRight) transform.Rotate(0, speed*Time.deltaTime, 0);
} I don't know which value Time.deltaTime assumes inside OnGUI - it should be the time since last OnGUI, but I'm not sure. To avoid problems, I placed the real rotation in Update and used two control booleans (rotLeft and rotRight) to communicate with OnGUI.
Answer by MoyaAdam · Oct 27, 2011 at 01:49 PM
try doing something like this
function OnGUI(){
var RotateSpeed:float; //speed to rotate at
var MyObject:GameObject; //the object to rotate
if(GUI.RepeatButton(SomeRect, sometexture)){
MyObject.transform.rotation.y+=RotateSpeed;
}
if(GUI.RepeatButton(SomeOtherRect, someOthertexture)){
MyObject.transform.rotation.y-=RotateSpeed;
}
}
if you want the code in C# ask me, and I dont know if it works, i'll test it when I get home for you
This is wrong! rotation is a quaternion, and rotation.y isn't the angle around y. You should use eulerAngles ins$$anonymous$$d:
$$anonymous$$yObject.transform.eulerAngles.y += RotateSpeed;
This will modify the angle around y.
In a quaternion, y is sin(angle/2) * ay where angle is the angle to rotate and ay is the y coordinate of the axis around which the rotation occurs. If you want to fry your brain learning about quaternions, read this article: http://www.gamedev.net/page/resources/_/technical/math-and-physics/quaternion-powers-r1095
Answer by rhose87 · Oct 27, 2011 at 02:49 PM
can i do this without rect ?
I would like both panels (rotate and zoom) to look the same (as zoom does, witch was done with gui textures).
I suppose you can draw the background texture with GUI.DrawTexture, then draw the buttons - they will be drawn over the background texture.
Your answer
Follow this Question
Related Questions
Rotating GUITexture 1 Answer
rotating guiTextures? 1 Answer
Camera rotation around player while following. 6 Answers
Cannot get GUIUtility.RotateAroundPivot working 1 Answer
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer