The question is answered, right answer was accepted.
Click a GUI button to rotate an object
Greetings! I'm very much new to the whole scene of game creation, so please be gentle with me! I've been up all night trying to figure out a way to do what I need done, but I've literally gone square-eyed from it haha.
Essentially, I need help with a script in order to do the following:
have a 'platform' (which is essentially a flat base) that I want to have rotate in 45degree sections upon clicking on a 3d text object. I know how to make the 3d text clickable, but I don't know if/how I can make it target the 'platform' when clicked on in order to rotate it.
-The idea is that there will be vehicles on the platform, and when you click the button the platform will rotate (or 'spin') to bring the next vehicle in to view (there will be 2 buttons, 1 for 'next' and 1 for 'previous' vehicle). I'd like to have the rotation happen quickly but smoothly.
This is what I'm currently using after trolling through what feels like a thousand other threads and Google searches looking for a solution. At the moment it's set to a Horizontal key (A or D) as I couldn't for the life figure out how to make it work on a OnMouseUp when clicking on a 3d text GUI object.
public class GUI_VehicleSelect : MonoBehaviour {
const float ROTATE_TIME = 1.0f; // seconds
const float WAIT_TIME = 1.0f; // seconds
const float ROTATION_MAX = 45.0f; // degrees
float timer = 0.0f;
void Update(){
if (Input.GetButton ("Horizontal"))
{
// increment timer
timer += Time.deltaTime;
// reset the timer after it goes past total time
while (timer > ROTATE_TIME + WAIT_TIME)
{
timer -= ROTATE_TIME + WAIT_TIME;
}
// rotate the object
if (timer < ROTATE_TIME) {
gameObject.transform.Rotate(Vector3.up * Time.deltaTime * ROTATION_MAX / ROTATE_TIME);
}
}
}
}
This SORT OF does what I need it do do. It will rotate in the 45degree segments like I want and it does it smoothly, but it will only rotate in one direction and if I ever let go of either Horizontal button it stops. I need it to do the full 45degrees as soon as it is triggered (regardless of whether I let off either the A/D keys or the mouse) and be able to rotate clockwise or anticlockwise (depending on which button is pressed).
I think that's all for now. I've always loved the idea of game creation and I've spent the last few days watching endless videos and following tutorials, but this has got me stumped.
Thanks very much to anyone who can help, or provide me with alternate ideas as to make my idea work!
The line "private var qTo : Quaternion;" produces two errors for me:
Assets/Rotate01.cs(8,25): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
and:
Assets/Rotate01.cs(8,37): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
Is this normal?
Stop posting your comments as new answers please.
Answer by robertbu · Aug 29, 2013 at 08:11 PM
Here's how I might approach the problem:
#pragma strict
private var qTo : Quaternion;
private var rotateSpeed = 65.0;
function Start() {
qTo = transform.rotation;
}
function Update() {
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
}
function OnGUI() {
if (GUI.Button(Rect(0,0,100,50), "Next")) {
qTo = Quaternion.AngleAxis(45.0, Vector3.up) * qTo;
}
if (GUI.Button(Rect(0,75,100,50), "Previous")) {
qTo = Quaternion.AngleAxis(-45.0, Vector3.up) * qTo;
}
}
Follow this Question
Related Questions
How to make the GUI Buttons consistent in screen while the background screen is rotatable? 0 Answers
How can I get a GUI button to Invoke Unity Events from my player action map? 0 Answers
More problems with arrow shooting 0 Answers
Is this possible?? (details below) [with pictures] 0 Answers
Reversed Rotation Problem 1 Answer