Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by Daumas18 · Apr 27, 2014 at 05:43 PM · quaternionoffseteulerlookrotation

Quaternion.LookRotation with an offset.

Hi everyone, this is my script :

 function Update () {
 
 var hit : RaycastHit;
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
     if (Physics.Raycast(ray, hit) && !Input.GetMouseButton(1) && !Input.GetMouseButton(2) && hit.rigidbody && PartPlacement.gui == false) {
                 
         transform.position = hit.point;
         transform.rotation = Quaternion.LookRotation(hit.normal);
                 
     }
     
     transform.eulerAngles.z = PartPlacement.rotation;
     
 }

I am trying to make a rotation offset for Quaternion.LookRotation using eulerAngles. The code that I have so far does its job perfectly until the transform is aligned with y axes. Then instead of being an offset static float "rotation" becomes a rotation speed value or in other words instead of degrees it becomes revolutions per second. I need Quaternion.LookRotation to stay in the script if possible.

It's fixed!

Full code:

 function Update () {
 
 var hit : RaycastHit;
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
     if (Physics.Raycast(ray, hit)) {
                 
         transform.position = hit.point;
         transform.rotation = Quaternion.LookRotation(hit.normal) * Quaternion.Euler(0, 0, PartPlacement.rotation);
                 
     }
     
 }
Comment
Add comment · Show 2
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 · Apr 27, 2014 at 06:02 PM 1
Share

From the reference:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations. When setting them to a new value set them all at once as shown above.

Reading and using eulerAngles this way is dicey. There are multiple eulerAngle representations for any given physical rotation, and the one you put in is not necessarily the one you get back out.

avatar image Daumas18 · Apr 28, 2014 at 03:55 PM 0
Share

Yeah, I googled (before posting this) and also found that setting axes separately in eulerAngles is no good, but I still could not fix the problem by my self, this is just as close as I can get... Can someone post a fixed script? I really have no idea what to change, still new to both Quaternion and eulerAngles.

1 Reply

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

Answer by gfoot · Apr 28, 2014 at 08:17 PM

Multiply the quaternion by an Euler rotation quaternion like this:

 transform.rotation = Quaternion.LookRotation(hit.normal) * Quaternion.Euler(0, 0, zAngle);

You can also specify a custom "up" axis to LookRotation, in case that suits your inputs better.

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 Daumas18 · Apr 29, 2014 at 01:10 PM 0
Share

That fixed the problem, thank you very much.

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

21 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

Related Questions

c# I want to lookat/rotate in only the Z axis so my object doesnt turn on its side/always faces camera 1 Answer

How To Prevent Axis Fliping When Rotating 0 Answers

Mimic other object's rotation on one axis (with offset) 0 Answers

Give the Quaternion Rotation an offset of 90° 0 Answers

Quaternion snaps to a rotation when moving (and with input)? 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