Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
16
Question by Quantum · Jun 15, 2011 at 05:32 PM · quaternionsrotationscalculatebce0051

Add or substract two quaternions/rotations

Hi,

I'm trying to instantiate a shot in front of my ship, but with a custom rotation relative to my ship's local coordinates.

So I tried this:

Instantiate(shotPrefab, transform.TransformPoint(Vector3(x,y,z)), transform.rotation + Quaternion.Euler(x,y,z));

But I get this error:

BCE0051: Operator '+' cannot be used with a left hand side of type 'UnityEngine.Quaternion' and a right hand side of type 'UnityEngine.Quaternion'.

So it says I can't add two quaternions.

What's the best way to solve this problem?

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

2 Replies

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

Answer by Peter G · Jun 15, 2011 at 05:34 PM

Multiplying should give you the result you want.

 transform.rotation * Quaternion.Euler();
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 Quantum · Jun 15, 2011 at 05:38 PM 0
Share

Thanks! =)

avatar image prestonmatterport · Apr 16, 2016 at 05:18 AM 5
Share

Just adding an additional note: with quaternions, it matters which one is on the left-hand/right-hand side. The rotation on the left of the multiplication symbol is applied first, then then one on the right is applied.

avatar image Ben_The_boxer · May 23, 2019 at 10:33 AM 0
Share

Who would have thought it, that little "*" could solve all my grief! Thank you so much for helping me with my understanding of C#

Instantiate(asteroid[randomAsteroid], spawn[randomSpawn].transform.position, spawn[randomSpawn].transform.rotation * Quaternion.Euler(0, Random.Range(-90, 90), 0));

Had got so close before when i tried using "+" and that was about 3 hours, 5 API descriptions, 35 Unity Answer pages and a youtube tutorial or two ago.

Thanks again.

avatar image
47

Answer by Cameron_SM · Mar 09, 2014 at 07:20 AM

I'm not sure why Peter G's response was voted as correct or voted up, it's simply wrong.

Multiplying a Quaternion by a vector returns another vector, and the vector you multiply by should be a direction vector (representing a direction from the origin), not a set of floats representing euler angles in degrees. The result of that multiply operation is that direction vector rotated by the Quaternion.

In order to add two quaternion rotations correctly you multiply them:

 Quaternion newRotation = transform.rotation * otherTransform.rotation

To subtract you need to multiply by the inverse:

 // Rotate back to the starting position
 transform.rotation = newRotation * Quaternion.Inverse(otherTransform.rotation) 


Alternatively, you can use Euler Angles and simply + (Add) or - (Subtract) the values then convert the result to a Quaternion.

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 robertbu · Mar 09, 2014 at 07:27 AM 3
Share

@Cameron - Quaternion.Euler() returns a Quaternion, not a vector.

avatar image robertbu · Mar 10, 2014 at 04:50 PM 1
Share

@Cameron - transform.rotation is a Quaternion, not a vector. Q Q results in a Q. Q V result in a V. V * Q is not allowed. @Peter G's answer is producing a Quaternion.

avatar image wibble82 · Mar 10, 2014 at 04:57 PM 2
Share

Peter's answer is correct. Both transform.rotation and Quaternion.Euler are quaternions. 2 quaternions multiplied together give you another quaternion (the one that represents their combined rotations).

avatar image Ady_M · Jan 11, 2019 at 09:24 PM 3
Share

Wow... 27 people up-voted this answer :|

I'm hoping it's because he demonstrated how to use Quaternion.Inverse() which answers the second part of the question.

avatar image qngnht · May 03 at 07:57 AM 0
Share

Thank you ! It took me 2 days how to fix Rotation with this in 1 minute.

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

11 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

Related Questions

Can I use Quaternions to get from one point to another? 1 Answer

Quaternions rotating left and right, not forward and back 1 Answer

4 rotations WTF 1 Answer

Relative Rotations and Position calculation 2 Answers

My interpretation of Random.rotation and Random.rotationUniform. Right or wrong ? 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