• 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 lazyfuntimesolutions · May 23, 2018 at 01:39 PM · rotationtransformquaternionlookrotation

Rotate object without storing facing

Hello, I'm rather new at using Unity, so I apologize if t$$anonymous$$s is trivial, but I haven't found a good solution for it online.


I've made a cube out of 6 planes, grouped them and added them to an empty game object with the location of the center of the cube and added my rotate script to it.

I want to move and rotate t$$anonymous$$s cube 90 degrees in one of four direction when pressing a key (just press, not holding down). I've got the translation working great, however the rotation is giving me trouble. I've tried :

   Quaternion toRotation = transform.rotation;
    toRotation *= Quaternion.Euler(rotationToadd);

And then Quaternion.Slerp() and it works well when moving only left to right or front to back, but when I move forward and then left the rotation is messed up.


What I would like to do is add or substract 90 degrees from the cube's rotation, no matter w$$anonymous$$ch way it's facing. E.g. add 90 degrees on Z axis => cube rotates =>new rotation is still (0,0,0).

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
1
Best Answer

Answer by Scribe · May 23, 2018 at 05:05 PM

Rather than using Quaternion.Euler try Quaternion.AngleAxis in t$$anonymous$$s case the angle will always be +/-90 and you choose the axis to rotate around. To rotate horizontally that would be Vector3.up for vertical it would be Vector3.right.


Edit So I forgot an important bit! What makes the AngleAxis method a useful one is that we can transform the axis into local space w$$anonymous$$ch isn't so easily done using a Eulerian representation. In the context of your code t$$anonymous$$s looks like:

     Vector3 axis = ...;
     Quaternion fromRotation = transform.rotation;
     Vector3 localAxis = transform.InverseTransformDirection(axis);
     Quaternion toRotation = transform.rotation * Quaternion.AngleAxis(rotationToadd, localAxis);

Hope that helps!

Comment
Add comment · Show 5 · 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 lazyfuntimesolutions · May 25, 2018 at 01:19 PM 0
Share

Thanks for your answer, that's definitely the method I need. However I'm unsure how to use it properly. This is what I have so far and the problem is still there. Quaternion fromRotation = transform.rotation; Quaternion toRotation = transform.rotation * Quaternion.AngleAxis(rotationToadd, axis);

         float elapsed = 0.0f;
         while (elapsed < duration)
         {
             transform.rotation = Quaternion.Slerp(fromRotation, toRotation, elapsed / duration);
             elapsed += Time.deltaTime;
             yield return null;
         }
         transform.rotation = toRotation;
avatar image Scribe lazyfuntimesolutions · May 25, 2018 at 04:47 PM 1
Share

Can you add the code you are using to find 'axis' please :)

avatar image lazyfuntimesolutions Scribe · May 26, 2018 at 03:57 PM 0
Share

Of course, thank you for taking an interest :) Here's all I've got:

   void FixedUpdate()
     {
         if (Input.anyKeyDown && !moving && !rotating)
         {
             Vector3 movementVector = Vector3.zero;
 
             if (Input.GetKeyDown(KeyCode.D))
             {
                 movementVector = new Vector3(1, 0, 0);
                 StartCoroutine(Rotate(-90, Vector3.forward, .2f));
             }
             else
             if (Input.GetKeyDown(KeyCode.A))
             {
                 movementVector = new Vector3(-1, 0, 0);
                 StartCoroutine(Rotate(90, Vector3.forward, .2f));
             }
             else
             if (Input.GetKeyDown(KeyCode.W))
             {
                 movementVector = new Vector3(0, 0, 1);
                 StartCoroutine(Rotate(90, Vector3.right, .2f));
             }
             else
             if (Input.GetKeyDown(KeyCode.S))
             {
                 movementVector = new Vector3(0, 0, -1);
                 StartCoroutine(Rotate(-90, Vector3.right, .2f));
             }
 
             if (!movementVector.Equals(Vector3.zero))
             {
                 startPosition = transform.position;
                 endPosition = startPosition + movementVector;
                 journeyLength = Vector3.Distance(startPosition, endPosition);
 
                 startTime = Time.time;
                 moving = true;
                 rotating = true;
 
             }
         }
 
         if (moving)
         {
             float distCovered = (Time.time - startTime) * moveSpeed;
             float fracJourney = distCovered / journeyLength;
 
             transform.position = Vector3.Lerp(startPosition, endPosition, fracJourney);
             if (transform.position.Equals(endPosition))
             {
                 moving = false;
             }
         }
     }
 
     IEnumerator Rotate(float rotationToadd, Vector3 axis, float duration)
     {
         Quaternion fromRotation = transform.rotation;
         Quaternion toRotation = transform.rotation * Quaternion.AngleAxis(rotationToadd, axis);
 
         float elapsed = 0.0f;
         while (elapsed < duration)
         {
             transform.rotation = Quaternion.Slerp(fromRotation, toRotation, elapsed / duration);
             elapsed += Time.deltaTime;
             yield return null;
         }
         transform.rotation = toRotation;
         rotating = false;
     }
Show more comments

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

129 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 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 can I multiply the rotation of a mirrored object? 1 Answer

How to instantiate on custom rotation? 1 Answer

Ball Rolling Animation Not Working Properly 0 Answers

Rotating an object towards target on a single axis 2 Answers

Basic AI Locked Axis 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