• 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
1
Question by Letail · May 20, 2017 at 09:12 AM · quaternionrotate objectrotation axisquaternion.lookrotation

Combining two Quaternion rotations together

I need to rotate a Quad so that it always faces the camera and spins around it's Z axis.

I use t$$anonymous$$s to rotate towards the camera. And it works fine:

 transform.rotation = Quaternion.LookRotation(-whereToLook);

And I use t$$anonymous$$s to spin it. And it works fine.

 transform.rotation *= Quaternion.Euler(0, 0, rotationSpeed * Time.deltaTime);

But I can't make both rotations work at the same time. I've tried multiplying them in every way and not$$anonymous$$ng works as intended.

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

3 Replies

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

Answer by Estecka · May 20, 2017 at 03:01 PM

If you run the first line every frame, lookRotation will constantly put the object right side up and reset its rotation around Z. The second line might make it rotate one step in the right direction, but the next frame it will be brought one step backward before being rotated again by only one step.

You could increment how much you rotate it everyframe like suggested above, but what I'd rather do is telling lookRotation to keep the current rotation around Z instead setting the object right side up; you can do that by giving it a second parameter :

 transform.rotation = Quaternion.LookRotation(-whereToLook, transform.up);
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 Kishotta · May 20, 2017 at 03:08 PM 0
Share

This is better/more correct. Multiply the above LookRotation by your (0, 0, RotSpeed) Quaternion and you're good to go.

avatar image Letail · May 22, 2017 at 01:42 PM 0
Share

YES! This worked! Thank you so much!

avatar image
0

Answer by Kishotta · May 20, 2017 at 02:41 PM

Try cac$$anonymous$$ng the local z rotation:

 private Quaternion localZRotaton = Quaternion.Identity;
 
 void Update () {
     localZRotation *= Quaternion.Euler (0, 0, RotationSpeed * Time.deltaTime);
     transform.rotation = Quaternion.LookRotation (-whereToLook) * localZRotation;
 }

The extra multiplication is needed to repeatedly rotate around the whereToLook axis, w$$anonymous$$le only applying that rotation once.

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

Answer by Knarhoi · Apr 25, 2020 at 07:43 PM

There's no need to use Quaternions. A simpler solution could be to use the object's forward direction. Example for a quad in 3D to always face the camera:

 transform.forward = Camera.main.transform.forward;

Or in case you don't want the quad to tilt:

 transform.forward = new Vector3(Camera.main.transform.forward.x, transform.forward.y, Camera.main.transform.forward.z);

You might need to invert the Z-axis, depending on your camera setup.

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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Child of a gameobject lookAt only around Y-axys 1 Answer

Quaternion LookRotation issue 0 Answers

Rotating an object in relation to its endpoints 1 Answer

Why is this rotation not performed as expected? 1 Answer

How would I interpret angular displacement from target rotation to max out at 180°? 3 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