Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
1
Question by rhysp · Jul 16, 2016 at 10:24 PM · rotationquaternionmathrotation detection

How can I work out the orientation of my rotatable cube via script?

I have a simple cube object, and the player can move this around the world. Movements are rigid/locked, as opposed to physics-based, so the cube will always have a flush face on the ground surface and be oriented with 90 degree snapping.

I am having trouble solving how I can determine the current state of my cube. Sure, I know the cube's quaternion, but I struggle to reliably translate this to, for example, knowing in my component script that "the left face is currently facing down, and is oriented 90 degrees".

My current solution works, via raycasting onto the cube (each face has a different hit collider), but I feel this is overkill to compensate for a nicer solution which presumably should be more efficient than raycasting.

Any advice is much appreciated.

Comment
Add comment · Show 1
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 rhysp · Jul 16, 2016 at 10:31 PM 0
Share

The orientation of the cube may be going down the wrong path, it isn't necessarily what I need, rather, I would like to know which face I would land on following any forward/back/left/right movements from the current orientation.

Eg. "the Left face is currently facing down, so moving forward would roll the cube onto it's Back face, moving left would roll the cube onto it's Up face"

1 Reply

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

Answer by rmassanet · Jul 19, 2016 at 03:09 PM

A quick and dirty solution would be to calculate the angle distance from the cube's up vector to all canonical vectors: Vector3.up, Vector3.left, etc... The canonical vector with the shortest distance to the up vector would tell you how the cube is rotated.

Comment
Add comment · Show 4 · 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 rhysp · Jul 20, 2016 at 04:47 AM 0
Share

Of course, that makes sense! Thank you.

To be honest, I wasn't aware that each GO had it's own orientation accessible (`transform.up`, etc.), but this was exactly what I was after. I was overcomplicating it trying to establish rotation, but this works ideally.

avatar image rmassanet rhysp · Jul 20, 2016 at 01:48 PM 0
Share

You're welcome. I'm glad it helped you.

avatar image $$anonymous$$ · Apr 01, 2017 at 03:17 PM 0
Share

Can I get an example of what this might look like in code, I'm new to Unity and I'm not sure how to go about accomplishing this.

avatar image $$anonymous$$ $$anonymous$$ · Apr 01, 2017 at 07:28 PM 0
Share

It's ok, got it to work. Thanks for the answer.

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

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

Related Questions

How to find a position's angle around an arbitrary axis? 1 Answer

Help with suns rotation 0 Answers

Enforcing clockwise direction for Quaternion.RotateTowards() 0 Answers

Rotate clockwise/counterclockwise using Quaternion.Slerp() 0 Answers

Making 2D Compass From Quaternion 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