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 lPVDl · Dec 04, 2015 at 08:35 PM · c#quaternionrotation axistrack

Track Quaternion local Y axis to position

What do I need is to track Quaternion to some point using it's local Y axis, how do I think it should work:

 Quaterion curQua; // Current and result rotation
 Vector3 curPos; // Current position
 Vector3 tarPos; // Target position
 Quaternion tarQua = Quaternion.LookRotation(tarPos-curPos) // Target rotation in global space
 Quaternion locQuaDelta = some black magic // Get tarQua in space of curQua
 curQua *= Quaternion.Euler(0, locQuaDelta.eulerAngles.y, 0); // Result

How should I 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

1 Reply

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

Answer by lPVDl · Dec 04, 2015 at 10:08 PM

Solved this that way: Getting target vector, projecting it on object's local space, getting angle, than rotating object by that angle:

 transform curTran; // Current transform
 Vector3 tarPos; // Target position
 Vector3 tarVect = curPos - tarPos; // Target rotation vector (z axis for object)
 tarVect = Vector.ProjectOnPlane(tarVect, curTran.up); // Getting projection
 float angle = Vector3.angle(curTran.forward, tarVect) // Rotation angle via local Y
 if (Vector3.Cross(Transform.forward, targetVector).y < 0) // If vectors have reverse order
                     angle = -angle; // Set negative angle
 curTran.rotation *= Quaternion.Euler(0, angle, 0) // Rotating.

Working for me, maybe there is some simpler way to do this, also, what will I do if I don't have transform?

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 Eno-Khaon · Dec 05, 2015 at 06:51 AM 0
Share

Well, if you don't have transform data, you'll need to supply some data yourself.

The key to this is that, as you mentioned, you're looking for a rotation on a local Y-axis. This means that you either need transform data or you need to be able to provide any two non-parallel directional vectors in order to reconstruct axes through Cross Products. If your axes aren't global, then they need to be defined.

You've solved the problem in one of a handful of ways by using Transform data. Either way, though, you need at least one other vector to be able to deter$$anonymous$$e a relative angle. Otherwise, your only remaining reference points will be world-space axes.

avatar image lPVDl Eno-Khaon · Dec 05, 2015 at 03:34 PM 0
Share

Can I get all necessary data from Quaternion, local forward and local upward vectors?

avatar image Eno-Khaon lPVDl · Dec 06, 2015 at 09:44 PM 0
Share

As long as any two major axes can be defined, you can create a correlation between them.

That said, Quaternion rotations tend to be based purely on being relative only to eachother. Quaternion.Identity is the baseline of having no rotation applied, but once rotations are applied, Quaternion rotations are very hard to reverse in full and, similarly, challenging to analyze.

Ins$$anonymous$$d, Euler angles are the easy means of keeping track of rotational data, but lack the versatility of Quaternions in that it becomes important to maintain a proper correlation between rotations. As a result, you can run into problems involving Gimbal Lock, especially when facing directly up or down.

Now, with all of that in $$anonymous$$d, your basis of rotation will come from any two defined axes. As long as there's a generalized pair of axes and any one is accurate, all three can be properly derived from that and, therefore, a proper correlation between objects' current rotations on any given axis can be calculated.

For example, knowing the local Z-axis and having a second, diagonal line across the Y- and Z-axes allows you to attain an accurate X-axis, and then rebuild an accurate Y-axis from them.

As an example of this:

 // Accurate
 Vector3 localForward = new Vector3(1, 0, 0);
 // Approximate
 Vector3 localUp = new Vector3(1, 1, 0);
 
 // Currently using a non-normalized vector, so normalizing the result
 Vector3 accurateRight = Vector3.Cross(localUp, localForward).normalized;
 // Both Vectors are normalized, so the result will be, too
 Vector3 accurateUp = Vector3.Cross(localForward, accurateRight);

In short, you will get your best information by making use of the Transform data of the character(s) in order to obtain proper axis-alignment. Based on that, you're already handling everything correctly as-is. Flatten the new angle onto a pair of axes (local transform's "up"), then deter$$anonymous$$e the angle and orientation of that angle.

The only caveat in general is the need for enough information to deter$$anonymous$$e relative differences in your data, and that's the need for at least two axes. An angle doesn't care what orientation it's at -- it's simply the angle between two vectors. So any other information needed when obtaining an angle needs to be provided separately.

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

32 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

Related Questions

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

Odd rotation on the wrong axis 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

issue after 360 degrees rotation 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