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 White8eard · Oct 30, 2014 at 08:49 AM · c#rotationquaternion

How to set rotation.y to predefined angles.

Here the character moves towards the point where you click, it successfully does that, but the problem is i want the character's rotation only to be setup in 0,90,180 and 270 degrees only,so i am trying to setup its value but the player is still uses default rotation.y that is any degree(26.333 for eg).

     void MoveDir(Vector3 position)
     {
         Vector3 dir = position - transform.position;
         Quaternion rotation = Quaternion.LookRotation(dir);
         rotation.x = 0;
         rotation.z = 0;
         rotationcheck = rotation.y;
         if(rotationcheck>45.0f && rotationcheck<=135.0f)
         {
             rotation.y = 90f;
         }
         else if(rotationcheck>135.0f && rotationcheck<=225.0f)
         {
             rotation.y = 180f;
         }
         else if(rotationcheck>225.0f && rotationcheck<=315.0f)
         {
             rotation.y = 270f;
         }
         else if(rotationcheck>315.0f && rotationcheck<=45.0f)
         {
             rotation.y = 0f;
         }
         players.transform.rotation = rotation;
         
         
     }
Comment
Add comment · Show 3
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 InfiniteDice · Nov 01, 2015 at 11:17 PM 0
Share

Finally an answer that worked.

Thx.

avatar image meat5000 ♦ InfiniteDice · Nov 01, 2015 at 11:17 PM 0
Share

Who's answer?

avatar image InfiniteDice meat5000 ♦ · Nov 02, 2015 at 03:51 AM 0
Share

Sorry... I gained help from thePersitor below... Here is what worked for me:

float shipHeading = 0f - pShipHullF.gameObject.transform.rotation.eulerAngles.y;

spinDial.rotation = Quaternion.Euler(new Vector3(0f, 0f, shipHeading));

I'm taking the y rotation of my ship and applying that rotation to a dial on a UI image, which rotates on the z. $$anonymous$$aybe this will help someone.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Oct 30, 2014 at 08:54 AM

First, you need to understand that transform.rotation is a Quaternion in which x,y,z are not angles. Please read this answer:

http://answers.unity3d.com/questions/799824/oafatwhy-does-transformrotationx-45-not-work.html

As for your question, you can do it this way:

  Vector3 dir = position - transform.position;
  float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
  angle = Mathf.Round (angle / 90.0f) * 90.0f;
  transform.rotation = Quaternion.AngleAxis (angle, Vector3.up);
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 Safforn · Oct 30, 2014 at 10:17 AM 0
Share

Yes, this works. I use it too.

avatar image
1

Answer by ThePersister · Oct 30, 2014 at 09:19 AM

Quaternions contain values between 0 and 1, therefore, if you set 90, 180, 270 or 0 in them directly, it'll go all drunk on you.

Instead, use EulerAngles for setting absolute values:

 // Store Transform as a variable instead of calling the hidden GetComponent :)
 Transform m_Transform;

 void Start()
 {
     // Fill m_Transform
     m_Transform = transform;
 }

 private void SnappedLookAt(Vector3 targetPos)
 {
     // Simple LookAt usage, remove unwanted axici if wished for
     m_Transform.LookAt(targetPos); 

     // Easy way to snap to a value, divide it so you get a number between 0 and 4, 
     // round that, then multiply by 90 resulting in 0, 90, 180, 270 or 360. :)
     float snappedValue = Mathf.RoundToInt(m_Transform.rotation.eulerAngles.y / 90) * 90;

     // Now set the rotation (x and z unchanged, y snapped)
     m_Transform.rotation = Quaternion.Euler(
         new Vector3(
             m_Transform.rotation.eulerAngles.x,
             snappedValue,
             m_Transform.rotation.eulerAngles.z));
 }

That should do the trick. I hope that helps, if you have anymore questions, feel free to ask, if this answer was satisfying enough, please accept it and move onwards with your epic game programming journeys! :D

Yours truly, ~ThePersister :)

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

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

Flip over an object (smooth transition) 3 Answers

Object Local Y-Up being forced to be World Y-Up 1 Answer

How to Change rotation while preserving local horizontal rotation 1 Answer

Smoothly Rotate 2D sprite 90 degrees on click 2 Answers

Quaternion to Euler conversion singularities 0 Answers


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