- Home /
GUI Button Question
I've got an object that I want to rotate. I've got a GUI button that I've created which handles this just fine. Trouble is, I want the object to continue rotating even after I've released the button. Is there a way I can accomplish this with a GUI button? Here' my simple code so far:
function OnGUI () {
if (GUI.RepeatButton (Rect (25, 25, 130, 30), "Deploy Aentenna")) {
transform.Rotate(Vector3.left * Time.deltaTime * 2);
}
}
Answer by Joshua · Jul 12, 2011 at 08:21 PM
To keep it spinning you could either give the object a rigidbody, turn of it's gravity, and set rigidbody.angularVelocity = Vector3( direction you want to rotation );
This way the physics engine will keep it spinning. But this could give some unexpected results. It's probably saver to start a coroutine.
var spinning : boolean = false;
function OnGUI()
{
if( GUI.Repeatbutton( Rect( 25, 25, 130, 30 ), "Deplay Anetnna" ) )
{
spinning = !spinning;
if( spinning )
Spin();
}
}
function Spin()
{
while( spinning )
{
transform.Rotate(Vector3.left * Time.deltaTime * 2);
yield;
}
}
This seems to work, but not all of the time. If I click quickly on the button, it stops when I release the mouse, or hold it down too long, same thing. In any case, it's a step in the right direction! Thanks for the info...
Your answer
Follow this Question
Related Questions
How to fire gui button on mouse right release 2 Answers
Look around when holding down mousebutton 1 Answer
GUI Button OnMouseUp? 2 Answers
Multiple rotate with mouse 2 Answers
How to rotate boject using UI button? 3 Answers