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 BryceBubbles · Dec 04, 2015 at 10:55 AM · rotationquaterniondirectionaxisworldspace

Rotate object so a specific axis faces another object, while also relative to the parent direction

I'm needing to rotate a specific axis (down, left, right etc) of object A towards object B's position, but relative to the rotation of a mutual parent/ancestor object, rather than to a world axis. This answer helped me solve the first part.

 Vector3 direction = objB.position - objA.position;
 Quaternion toRotation = Quaternion.FromToRotation(Vector.down, direction);
 objA.rotation = toRotation;

Vector.down (negative y) will point at object B, except that the forward (z) axis will always point towards world forward (z). I have tried a lot of different things, but I haven't been able to crack it.

So how do I modify this to point the specific axis at the vector while keeping the forward (z) axis facing the forward axis of a shared parent/ancestor object? Thanks.

Comment
Add comment · Show 3
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 BryceBubbles · Dec 05, 2015 at 06:49 AM 0
Share
  • Do I need to do another rotation afterwards the FromToRotation that aligns the objects z with the ancestor z? This is mostly what I've been trying, but with my limited knowledge it seems to get very messy.

  • Or can I some how modify this FromToRotation method to achieve it?

  • Or is there another method I should be using that does this elegantly? Thanks

avatar image Eno-Khaon · Dec 05, 2015 at 07:01 AM 0
Share

Okay, let's see if I have all of this in order:

You have a parent object (Let's call it Object C).

This parent has two children, Objects A and B.

A needs to be oriented so that its negative Y-axis is pointed at B, but then its alignment otherwise matches its parent, C?

Or, alternately, is the parent intended to take priority, so that A faces the same way as C, but then its Y-axis faces away from B as much as possible otherwise?

Beyond this, anyway, a "FromToRotation()" is intended to be multiplied by another Quaternion to apply a relative change. For instance:

 objA.rotation = objA.rotation * toRotation;

... or maybe it's

 objA.rotation = toRotation * objA.rotation;

ins$$anonymous$$d? I always forget which order a Quaternion's non-commutative order of operations is under various criteria.

avatar image BryceBubbles Eno-Khaon · Dec 05, 2015 at 10:46 AM 0
Share

A and B are the child of C yes, and A's y-axis will be pointing at B with the code I posted. A's Z-axis should then point at the Z-axis, not of C, but another ancestor object further up the hierarchy - the root if you will. C's rotation could be anything, but it can be disregard I think (unless using localRotation - which I tried also).

The code I showed perfectly rotates A's Y to point at B, but A's Z points at world Z and changes as the root rotates, so I'd like to point it to the root objects Z ins$$anonymous$$d. I think I need to do a rotation around the Y after the above code (as I can manually do that in the inspector to line it up), but I can't work out how to apply this correctly in code - 3d rotation does my head in.

I've seen an explanation of the "relative change" code you mention elsewhere and had tried it, but this makes A no longer point at B properly, and doesn't actually take the root objects rotation into account.

Thanks for your time.

1 Reply

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

Answer by Eno-Khaon · Dec 06, 2015 at 10:11 PM

Well, if you need to base it on the rotation of a base object higher up (or at the top?) of the hierarchy, you'll need information on that, since any given local rotation is relative only to its immediate parent.

For example:

 // Returns the Transform at the very top of the hierarchy
 Transform GetParent(Transform current)
 {
     Transform result = current;
     if(current.parent != null)
     {
         result = GetParent(current.parent);
     }
     return result;
 }

Otherwise, you'll need a stricter definition which would require tags, layers, manual definition or anything similar.

Now, for the resulting rotation, then, you'll want to do something along the lines of:

 // This ensures that A's down points at B's position
 Quaternion rotationOffset = Quaternion.AngleAxis(90.0f, Vector3.right);
 // Point straight towards B with secondary emphasis on the root's forward vector
 // Then, apply offset to align the downward axis instead
 objA.rotation = Quaternion.LookRotation(objB.position - objA.position, objRoot.forward) * rotationOffset;

As an alternative, if facing forward is more important than downward facing towards B, implementation is a much simpler:

 objA.rotation = Quaternion.LookRotation(objRoot.forward, objA.position - objB.position);
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 BryceBubbles · Dec 07, 2015 at 10:26 PM 0
Share

Thanks, this got me where I needed.

$$anonymous$$y objects class has a reference to it's root, so I was sorted there.

I found that it was -90 to point the down axis at B.

And then to point the left or right axis at B ins$$anonymous$$d, it was easiest to just add another AngleAxis rotation on the forward axis. Here's the full answer for my needs:

 //A rotation to point A's down axis at B's position
 Quaternion rotationOffset = Quaternion.AngleAxis(-90f, Vector3.right);
 
 //Add an extra rotation if you need to point A's left or right axis at B ins$$anonymous$$d
 if (axisToPoint == Vector3.left)
     rotationOffset *= Quaternion.AngleAxis(90f, Vector3.forward);
 else if (axisToPoint == Vector3.right)
     rotationOffset *= Quaternion.AngleAxis(-90f, Vector3.forward);
 
 Vector3 direction = objB.position - objA.position;
 objA.rotation = Quaternion.LookRotation(direction, objRoot.forward) * rotationOffset;

It might be possible to combine the left/right axis rotation into the first axis rotation, but I couldn't work that out.

I also just realised that I could have used the code in my question, if I just aligned the root object with Quaternion.identity, did the child rotations, and then rotated the root back to where it was. This would only work if you weren't doing it at run time while the object was visible (which I'm not). This answer is more robust though.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Creating a multiple part turret what locks onto certain axis. 4 Answers

90 Degree stopping rotation on y-axis issue 0 Answers

Unity Simulate Local Rotation 0 Answers

3D nested Turret Prefab Rotation 1 Answer

allows users to view 360º content without a VR headset 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