• 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
2
Question by Meltdown · Jul 28, 2011 at 04:16 PM · rotationangle

Getting the correct rotation problem

I have a slot machine with 3 reels. Each reel is a simple cylinder divided into 12 x 30 degree segments, with each segment having a picture. Match the pictures and you win. When I pull the handle, I basically have a spin timer that rotates the reels until the time stops i.e

 directionX = 1800;
 
 function Update()
 {
     if(spin){
         transform.Rotate (directionX * Time.deltaTime, 0, 0);
         spinTimer -= Time.deltaTime;
         currentAngle = transform.eulerAngles.x;
         }
   }

The spinning and stopping all works nicely, I have code for that, but what stumps me here is, if I rotate the reel within Unity's editor, i.e I set the rotation to 90 in the editor, I see a totally different picture on the reel than to the one if the reel had been rotated in game mode when the rotation of the cylinder returned by Unity is also 90. (Or any other rotation for that matter). The value of 'currentAngle' which I get from the reels x eulerAngle don't match. In other words a different picture on the slot shows than what should according to the rotation of the wheel in the editor.

I've tried almost every way in the book I can to get the angle, using Quaternions and rotating the wheel using other methods, but I cannot for the life of me get the rotation of the wheel in the editor to match that of the value returned while the game is running.

I need this value so I can set the wheel's rotation to snap to 30 degree intervals so if it lands close to the picture the rotation is snapped so the picture is displayed in the middle.

There is no rigidbody attached to the reel at all, it is simply the rotation of the mesh.

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 Herman-Tulleken · Jul 28, 2011 at 05:13 PM 1
Share

What I find useful for debugging in situations like this is to plot the values. $$anonymous$$eep track of your own euler x, write this value and the actual euler x out to file, something like this:

 stream.WriteLine(Time.timeSinceLevelStarted + "," myCurrentAngle + "," + currentAngle)

(open and close the stream appropriately)

open this file in Excel, and plot a graph of the two variables against the time in the first colum. You should be able to see instantly what is going on: perhaps they are out by a factor, perhaps rotate sets the actual degrees (and not relative).

(You can make a general system for this kind of debugging: http://devmag.org.za/2011/02/09/using-graphs-to-debug-physics-ai-and-animation-effectively/ )

avatar image Meltdown · Jul 28, 2011 at 05:21 PM 0
Share

Thanks for taking the time to comment Herman. I thought I was just doing something noobly wrong. You're right, some in depth debugging is needed. I'll plot everything out and see if I can pick up any discrepancies.

2 Replies

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

Answer by aldonaletto · Jul 29, 2011 at 03:51 AM

I don't know what's causing this discrepancy, but I would do it in a different manner: have a number rotating in module 12, multiply it by 30, convert to quaternion and set transform.rotation. It's better than using Rotate freely and try to guess the position using eulerAngles (they can show very strange angles sometimes).
The script below uses this principle: angle rotates in modulo 12 and is used to set the transform rotation. When the reel slows down and stops, angle is rounded up and stored in number to indicate the number drawn (it's 0 during the drawing):

var rotSpeed: float = 18; // speed in segments per second var rollTime: float = 2.5; // time to roll until stop var number: int; // number when reel stopped private var angle: float = 0; private var time: float = 0;

function Update(){ if (Input.GetKey("r")){ time = rollTime; // keep rolling at full speed while r pressed number = 0; // number invalid while rolling } if (time > 0){ var t = time / rollTime; // reduce speed during rollTime angle = (angle + t rotSpeed Time.deltaTime) % 12; transform.rotation = Quaternion.Euler(angle * 30, 0, 90); if (t < 0.2){ // if speed below 20% rotSpeed... if (angle % 1 < 0.05){ // roll until segment position error < 5% number = Mathf.Ceil(angle); // save number 1 to 12 time = 0; } } else { time -= Time.deltaTime; } } }

Comment
Add comment · Show 6 · 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 Meltdown · Jul 29, 2011 at 06:05 AM 0
Share

Thanks for the suggestion, when I get home I'll give it a bash and let you know. Yeah the angles are definately inconsistent. This might be what I need.

avatar image aldonaletto · Jul 29, 2011 at 05:14 PM 1
Share

I forgot to mention: in my tests I used a cylinder, and rotated it 90 degrees around Z to get it in vertical position - that's why Quaternion.Euler has 90 degrees at the Z axis.

avatar image Meltdown · Jul 29, 2011 at 07:29 PM 0
Share

Dude I cannot thank you enough for that script. That really helped me out of a very stuck situation. $$anonymous$$uch appreciated.

avatar image aldonaletto · Jul 30, 2011 at 12:06 AM 0
Share

I know the feeling: I had a hard time once trying to track the angle of a Rotate'd propeller - that's why I lost my faith in eulerAngles after Rotate - you definitely can't trust in this combination.

avatar image Meltdown · Jul 31, 2011 at 07:47 AM 0
Share

In Quaternions we shall trust :)

Show more comments
avatar image
0

Answer by DaveA · Jul 28, 2011 at 05:20 PM

I'm guessing that in the Editor you are affecting the localRotation and this code affects the world rotation?

Comment
Add comment · Show 2 · 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 Meltdown · Jul 28, 2011 at 08:17 PM 0
Share

Hi Dave, whether I use Space.World or Space.Self in my transform.rotate method, either way the angles returned in my log file or in the gui don't correspond with what they look like if I set that value in the editor. Any other ideas? This one has got me stumped :(

avatar image Meltdown · Jul 28, 2011 at 08:20 PM 0
Share

When the rotation is set to Space.World The x rotation in the inspector for the object when the 1 reel stops shows 300.0218 The x eulerangle for the object in my gui/logging shows 329.9782

When the rotation is set to Space.Self The x rotation in the inspector for the object when the 1 reel stops shows 301.7924 The x eulerangle for the object in my gui/logging shows 328.2076

Any idea why there is this constant discrepancy?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

rotation x changes all angles? 3 Answers

local rotation per axis between frames? 1 Answer

Limit local rotation 6 Answers

Make object only rotate in 90 degree increments 0 Answers

Vector2.Angle questions and confusion. 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