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 /
  • Help Room /
avatar image
0
Question by Incognito357 · Mar 01, 2016 at 08:11 AM · quaternionsrelative rotationrelative position

Match objects rotation to that of another objects relative rotation

I have 4 objects, two planes and two arrows. One arrow points at one of the planes, and I want the second arrow to match the first arrow, but relative to the second plane. What I mean by this, is if the first arrow is moved 1 unit to the right relative to the first plane, the second arrow should move 1 unit to the right relative to the second plane. I have gotten the positions to work out correctly, but I can't figure out how to match the rotations. Some axes of rotations work correctly some of the time, but once one of the planes are rotated, the second arrow's rotation gets more out of sync with the first.

Here's the code that I'm using to test this, with the best I could figure out:

 [ExecuteInEditMode]
 public class RotationTest : MonoBehaviour
 {
     public GameObject target;
     public GameObject camera;
     public GameObject arrow;
 
     void OnWillRenderObject()
     {
         if (!target || !camera || !arrow) return;
 
         arrow.transform.position = target.transform.TransformPoint(transform.InverseTransformPoint(camera.transform.position));
         arrow.transform.rotation = Quaternion.Inverse(camera.transform.rotation) * transform.rotation * Quaternion.Inverse(target.transform.rotation);
         //arrow.transform.eulerAngles = new Vector3(arrow.transform.eulerAngles.x, -arrow.transform.eulerAngles.y, arrow.transform.eulerAngles.z);
     }
 }

I put this script on the first plane, the first arrow is 'camera', the second plane is 'target', the second arrow is 'arrow'. When both planes are aligned exactly, the second arrow should be exactly the same position and rotation as the first. The commented out lines cause the arrow to match each axis of rotation individually (I think), but when multiple axes are rotated, it gets out of sync.

When both planes are facing the same direction, simply setting the rotation of the second arrow to the first arrow's rotation works correctly, but once the first plane is rotated this method falls apart.

I've been at this for quite a while, trying different combinations of Quaternion multiplications, but I can't seem to figure it out. I originally tried doing the same thing with the rotation that I did with the position (using TransformDirection), but I couldn't get that working either. Any help at this point is much appreciated.

[Edit]: I was able to get the arrows to match when the two planes are at the same position and orientation using the following code

 arrow.transform.eulerAngles = target.transform.TransformVector(transform.InverseTransformVector(camera.transform.eulerAngles));

however, this breaks once either one of the planes are rotated. It does the same thing as setting their rotations equal to each other, but it differs when a plane is rotated, causing the arrow to rotate in very strange ways. Trying this with same idea with TransformDirection and the camera's forward vector does not yield the expected results.

Edit2: I was able to get the arrow to match correctly when the planes are rotated in only a single axis of rotation (about the Y axis) with arrow.transform.eulerAngles = (target.transform.eulerAngles - transform.eulerAngles) + camera.transform.eulerAngles; but this still breaks when one or both of the other axes are rotated. Here are a few pictures describing what I'm trying to achieve, and what is actually happening with the latest code: http://imgur.com/a/MVI8x

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
1

Answer by Tsilliev · Jul 04, 2017 at 10:43 AM

I have an object that I want to copy my camera's y rotation, here is how:

transform.localEulerAngles = new Vector3(0.0f,MyCamera.transform.localEulerAngles.y, 0.0f);

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 COLiTRON · Jan 05, 2018 at 03:14 PM 0
Share

THAN$$anonymous$$ YOU! I've been trying to match one axis of rotation with no luck (Because I'm completely new to scripting, and totally clueless.) and your script does the trick.

At last I can stop banging my head against the wall. Now onward to the next problem that I will bang my head against the wall over!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How would I orient VTOL engines relative to the direction of a ship's movement? 1 Answer

Smooth, Frame independent, relative rotation? 0 Answers

Have object reach a location relative to another object's rotation 0 Answers

Place object rotated relative to another object? 0 Answers

How to Quaternion Slerp back to Original Position but Keep Current Heading? 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