Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Reverend-Speed · Sep 17, 2019 at 08:18 PM · rotationquaterniontransform.rotationalignment

How can I align axis with quaternion.fromToRotation?

Hey folks, going somewhat crazy here. Trying to align two transforms, so that a prefab can be instantiated and made to stand at a particular parent empty, standing upwards along a particular axis of the parent empty.

In the following image, I have a basic tree whose Y axis should aligned to the parent transform's -Z (minus z) axis.

I desire that the tree should be standing with the trunk in line with the blue axis of the parent transform, but 'growing' in the opposite direction to the blue arrow (the green ball of the 'tree' should be away from the transform's origin in the opposite direction to the blue arrow).

alt text

When the tree is instantiated, it is assigned the rotation of its parent transform and all axis of both objects are aligned.

I identify the rotation between the axis and apply it to the tree's transform, following the example on this page. While that code works on a simple test project, I can't get it to work in my work scene. At the moment, my code reads as follows:

 tempTree.rotation = Quaternion.FromToRotation(tempTree.up, -treeArray[i].transform.forward);

I have also attempted using the code so that the rotation from Quaternion.FromToRotation is applied to the transform.rotation through multiplication.

 tempTree.rotation *= Quaternion.FromToRotation(tempTree.up, -treeArray[i].transform.forward);

In both cases, my result is as seen in the attached image - the tree is rotated to a seemingly arbitrary angle, entirely unsuitable for the project.

Please, if you can, help me out with this one. Time is short, and I'm going out of my tree. =D

rotationtrubs01001rs.jpg (13.7 kB)
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
0
Best Answer

Answer by Reverend-Speed · Sep 18, 2019 at 09:21 AM

It turns out that the correct code is...

 tempTree.rotation = Quaternion.FromToRotation(Vector3.up, -treeArray[i].transform.forward);

This is basically, exactly like the example code from the documentation. I had been under the impression that I was supposed to take the tree's rotation and turn it towards the target rotation, but apparently not so... To quote Antistone on the Unity forums,

You are setting the tree's rotation in world space, so you want the rotation that would move world up to point in a certain direction, not the tree's current up (which you are overwriting).

I'm honestly still a bit confused about this, but you can't argue with results...!

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

146 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 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 to combine multiple rotations at once? 0 Answers

Follow rotation on Y axis ONLY clockwise/counterclockwise. 1 Answer

The best way for working with rotation in 2d game 1 Answer

Quaternion.identity giving outlier result? 1 Answer

Aiming to gameobject 2 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