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
0
Question by JayFitz91 · Jul 24, 2014 at 12:56 PM · rotationquaternionaccelerometer

Manipulate an objects rotation

I am using the accelerometer to control my object, it sits in one spot and the accelerometer controls its rotation, turning left and right. At the moment, it spins the entire way around, doing a 360.

What I would like is to limit its turning so that I can never look behind my object, hopefully the code explains it better:

 //This turns the object but allows 360 turning
 var accelDirection = Input.acceleration.x; 
             transform.Rotate(Vector3.up * accelDirection * 5);

 //I would like something like
 if(myObject.transform.rotation >= 90)
 {
     //Stop turning
 }

As it stands, the editor is saying I can't compare a Quaternion and an int (obviously), so is there a way I can set up what I'm looking to achieve?

Any help would be appreciated

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 DajBuzi · Jul 24, 2014 at 01:00 PM 0
Share

Because you cant compare 2 objects of different types. Rotation have 3 member variables in it: X, Y, Z try doing it like this:

 if(obj.transform.rotation.y >= maxRotAngle)
 {  /* StopTurning */  }
avatar image JayFitz91 · Jul 24, 2014 at 01:55 PM 0
Share

Cheers for the input lads, i'll test it out when I get home

1 Reply

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

Answer by robertbu · Jul 24, 2014 at 01:49 PM

'transform.rotation' is a quaternion, and it's x, y, z, and w values are not angles. You can use transform.eulerAngles as long as you are only rotating on the 'y' axis.

Comment
Add comment · Show 3 · 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 JayFitz91 · Jul 24, 2014 at 05:00 PM 0
Share

@robertbu, eulerAngles proved the most effective method for me. If you convert your comment into an answer i'll select it, cheers

avatar image robertbu · Jul 24, 2014 at 05:10 PM 0
Share

Restricting rotations can get ugly in Unity. As long as the you are only rotating on the 'y' axis, and the 'x' and 'z' rotations are at or near 0.0, you should be fine checking eulerAngles. For more complex rotation situations, you will find that checking eulerAngles will fail, and therefore you must take other approaches.

avatar image JayFitz91 · Jul 24, 2014 at 05:21 PM 0
Share

For what i want,I'm only rotating the y-axis, x and z won't be touched. But thanks for the info, good to know

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate with raw gyro data. 0 Answers

How can i rotate a object more than once (90 degrees)? 1 Answer

Yaw Pitch Roll order? 0 Answers

Can this be rewitten to control axis locally instead of globally? 1 Answer

Rotate object permanently around Y-axis while rotating around Z-axis to face mouse? 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