Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 08, 2016 at 05:49 PM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted.

avatar image
1
Question by AngryDome2342 · Aug 29, 2013 at 07:51 PM · rotategui-button

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JefferyWright · Jan 10, 2016 at 06:45 AM 0
Share

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?

avatar image Le-Pampelmuse JefferyWright · Jan 10, 2016 at 06:46 AM 1
Share

Stop posting your comments as new answers please.

1 Reply

  • Sort: 
avatar image
1
Best Answer

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;
 
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryDome2342 · Aug 30, 2013 at 11:37 AM 0
Share

That does it! Thank you very much. :D

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges