Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ROMgame · Apr 10, 2014 at 09:05 PM · quaternionmatheulerangles

Aligning two game objects position/rotation

I have two empty game objects (with different parents) at different positions and with different rotations. I want to align them such that the second game object is placed in the same position as the first game object and with its z axis (i.e. the editor blue arrow) facing in the complete opposite direction to the first game objects z axis. The second game objects original z rotation should, however, be retained.

I tried doing this by:

  • Storing the second game objects local z rotation

  • Setting the second game objects position/rotation to the same as the first game object

  • Rotating the second game objects 180 degrees on the x axis so the "blue arrows" of the objects are facing in opposing directions

  • Finally I set the second game objects local z rotation back to it's previous z rotation.

The problem is this doesn't work as i'd expect, the rotations are often wrong. The only reason I can think of is because of the way euler angles work where you can have several different euler values for one quaternion (so resetting the second game objects local euler to its previous value won't work).

Does anyone know how I might solve this problem with quaternions?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by vvolfster · Nov 10, 2016 at 08:13 AM

I did something very similar to this. I know this is a year late but I just started Unity and maybe this will be helpful to someone else. I was aligning my two objects on their blue axis in local mode. You can use the forward vector for this. If there's some other axis you want to align on, use up or right vectors of the transform.

Quaternion.FromToRotation (transformA.forward, transformB.forward * -1);

This will give you how much A needs to rotate to align itself with B. You also need to set their transform.positions to be the same afterwards.

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
avatar image
0

Answer by GeorgesAbitbol · Apr 11, 2014 at 12:03 AM

Did you try those ?

  • Transform.RotateAround

  • Transform.LookAt (using reversed object 1's transform.forward + transform.position)

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 ROMgame · Apr 11, 2014 at 06:37 AM 0
Share

Unfortunately I have no problem setting the second objects rotation to the first objects rotation, my real problem is setting the z rotation back to it's previous value :)

avatar image GeorgesAbitbol · Apr 12, 2014 at 01:18 AM 0
Share

If you use rota$$anonymous$$round the Y axis, you should preserve the Z rotation.

I dont really understand what are the constraints in your project, so im not sure what the solution should be. Could you explain what you are trying to do with an explicit example ? It sounds like your are trying to plug two objects together, is it like power plugs ?

avatar image ROMgame · Apr 12, 2014 at 06:39 AM 0
Share

So the problem with "RotateAround" is what angle do I put into it? The y and z axis values of my objects sometimes "flip" because of how Euler angles work so I couldn't simply work out the difference between object 1 and 2s y rotation for example.

As for what I'm doing, I have two planes, each with the blue arrow facing perpendicular (or up) to the plane. I want to align the planes such that they are flat against one another whilst preserving their individual rotations about the blue arrow. They could be placed at any location, so long as they are aligned in this way.

Thanks for your continued support, I've been trying to get to grips with this for days!

avatar image
0

Answer by Vylax · Nov 10, 2016 at 11:06 AM

Try to use this:

 var pos2Keep : float = 10; //the position (x or y or z) that u wanna keep for the object u're going to align
 var obj1 : Transform; //object u're aligning the other one to
 var obj2 : Transform; //object u align
 
 function start () {
 pos2Keep = obj2.position.x; //or y or z
 obj2.position = Vector3(pos2Keep, obj1.postion.y, obj1.position.z);
 obj2.rotation = Vector3(obj1.rotation.x, obj1.rotation.y, obj1.rotation.z);
 }
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 Vylax · Nov 10, 2016 at 11:07 AM 0
Share

oh yeah nice necro

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

24 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

Related Questions

[SOLVED]aligning rigidbody to be "level" with world's X and Z coords. How can I make sure it faces the same way (Y coord)? 0 Answers

Camera viewport transformation from one world to the rotated world. 1 Answer

Apply game object rotation using relative VR controller rotation? 2 Answers

Is my if statement changing my Euler angle value? 1 Answer

Comparing lerping quaternion 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