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 /
avatar image
1
Question by Kristov · Jan 20, 2012 at 10:25 AM · rotationjavascriptonce

Rotation script working only in one instance

Hi all,

Okay, here's one that's got me stumped. It might be the simplest problem, but I need new eyes looking over it; I can't figure out what's wrong.

So the situation is this: I've started to make an in-game menu using objects such as cubes with OnMouseDown() functions as opposed to using the Unity GUI. Because of that I've made it so each section of the menu (Main menu, options menu, etc) are in a different space in the scene, and when one transitions to another, the camera rotates to look at the new menu. For example, I click the 'Options' button in the Main Menu, and the camera rotates 90 degrees down to see the Options Menu.

Here's the relevant code attached to one of my button objects:

 private var mainCamera : GameObject;
 
 function Start()
 {
     mainCamera = GameObject.Find("Main Camera");
 }
 
 function OnMouseDown()
 {
     mainCamera.SendMessage("LookUp");
 }

The only difference in the code between the two buttons is one sends a "LookUp" message to the camera, and the other sends a "LookDown" message. And the code attached to my Main Camera object:

 var rotateSpeed : float;
 private var endAngle : int;
 private var lookUp : boolean;
 private var lookDown : boolean;
 
 function LookUp()
 {
     endAngle = transform.eulerAngles.x - 90;
     if(endAngle == -90)
     {
         endAngle = 270;
     }
     lookUp = true;
 }
 
 function LookDown()
 {
     endAngle = transform.eulerAngles.x + 90;
     if(endAngle == 360)
     {
         endAngle = 0;
     }
     lookDown = true;
 }
 
 
 function Update()
 {
     if(lookUp == true)
     {
         transform.eulerAngles.x -= rotateSpeed;
         if(transform.eulerAngles.x > endAngle - 1 && transform.eulerAngles.x < endAngle + 1)
         {
             lookUp = false;
             transform.eulerAngles.x = endAngle;
         }
     }
     if(lookDown == true)
     {
         transform.eulerAngles.x += rotateSpeed;
         if(transform.eulerAngles.x > endAngle - 1 && transform.eulerAngles.x < endAngle + 1)
         {
             lookDown = false;
             transform.eulerAngles.x = endAngle;
         }
     }
 }

(Lerping and such never seems to work for me, so this is a crude workaround)

This all works perfectly fine from the first menu, going both up and down, however when I try to go back from either of the other menus (using the same scripts attached to different objects) it never works. The camera jitters once then stops completely.

I've been trying to figure this one out for hours now, and I hate leaving a problem unsolved before bed, but its got to be done, I'm at my wits end!

Thanks for your help!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jan 20, 2012 at 11:24 AM

You can't rely on eulerAngles to make rotations this way: eulerAngles are actually transform.rotation converted to 3-axes format, and since this is a redundant representation, different x, y, z combinations may be returned at some points of the rotation.
The best way to do that is to save the initial eulerAngles in a Vector3 variable at Start, then "rotate" the desired angle by math, assigning it to eulerAngles each Update. You could also use Mathf.MoveTowards to change the angle x smoothly, and multiply rotateSpeed by Time.deltaTime to make it framerate independent. Using these tricks, things get much simpler:

var rotateSpeed : float; // degrees per second private var endAngle : int; private var euler : Vector3;

function Start() { euler = transform.eulerAngles; // get the initial angles endAngle = euler.x; // initialize endAngle to the current x }

function LookUp() { endAngle -= 90; }

function LookDown() { endAngle += 90; }

function Update() { euler.x = Mathf.MoveTowards(euler.x, endAngle, rotateSpeed * Time.deltaTime); transform.eulerAngles = euler; // update the actual eulerAngles } This code rotates endAngle 90 degrees up or down each time you press the buttons, and the euler variable follows it with the desired speed. MoveTowards is cool because it moves from the current euler.x value to endAngle at the desired speed, and stops moving exactly at the destination value.

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 Kristov · Jan 20, 2012 at 11:59 PM 0
Share

Thanks so much for that, worked perfectly! Seems I have a lot to learn still, even on the basics ^-^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Realtime Object Rotation on iPad 0 Answers

Smooth rotation in 90° increments 0 Answers

How To Check If X Rotation Is Between Two Numbers?? 1 Answer

space flying system 3 Answers

Rotation Jerks when Copying From Different Object 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