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 LostMortal · Dec 13, 2016 at 02:43 AM · c#rotationscripting problemscripting beginnercopy

Copy Rotation in script C#

Hi, Unity Community. I would like to know if there is any kind of feature like Copy Rotation from another object? or if there is another way to Copy Rotation and an offset let's say

 This is some values that would be seen on the inspector
     GameObject Transform:
     Rotation: X => 0 ; Y => 45; Z =>0;
     SecondGameObject that would follow the rotation of the GameObject (In the second GameObject I need it with an offset )
     Rotation: X => 0 ; Y => 45; Z =>0;

In the next Script I have the following code written:

     void Update() {
             CopyRotation();
     }
 
     // Copy Rotation from GameObject
 // default(Vector3) like Vector3(0, 0 , 0)
     public void CopyRotation(GameObject Target, Vector3 Offset = default(Vector3))
     {
         /*transform.rotation = Quaternion.Euler(
             Target.transform.localRotation.eulerAngles.x,
             Target.transform.localRotation.eulerAngles.y,
             Target.transform.localRotation.eulerAngles.x
             ); */
        // This is working if I need to copy the Rotation too.
        // transform.rotation = Target.transform.localRotation;
        Offset = Vector3(0 ,10, 0);
        // I need to use the Y rotation starting with 10 degrees
       // But let's say I need the Z axis rotation starting with 10 degrees
       //Offset = Vector3(0 ,0, 10); // is it possible?
        transform.rotation = Target.transform.localRotation + Offset;
     }

so let's say

 This is some values that would be seen on the inspector
     GameObject Transform:
     Rotation: X => 0 ; Y => 45; Z =>0;
     SecondGameObject that would follow the rotation of the GameObject (In the second GameObject I need it with an offset )
     Rotation: X => 0 ; Y => 55; Z =>0;

I am not sure if explained well what idea I want to achieve in this script.

Keep in mind this Script is not working

The next function works very nice, it is like a parent but without following the parent rotation.

 // Copy Location from GameObject
     public void CopyPosition(GameObject Target, Vector3 Offset = default(Vector3))
     {
         transform.position = Offset + Target.transform.localPosition;
     }

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
2
Best Answer

Answer by brunocoimbra · Dec 13, 2016 at 02:52 AM

To achieve something like that with rotation:

  public void CopyPosition(GameObject Target, Vector3 Offset = default(Vector3))
  {
      transform.position = Offset + Target.transform.localPosition;
  }

You can do that:

  public void CopyRotation(GameObject Target, Vector3 Offset = default(Vector3))
  {
      transform.rotation = Quaternion.Euler(Offset) * Target.transform.localRotation;
  }

Quaternion.Euler transform the given euler angles (andles in X, Y, and Z) into a Quaternion rotation.

Also, to "add" 2 Quaternions you should use the "* (multiply)" operator.

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 LostMortal · Dec 15, 2016 at 04:20 AM 0
Share

@brunocoimbra For some reason the OFFSET value in the second object and the rotation values shown in the inspector are differents. Do you know the closest reason?

The copy rotation works perfectly alt text

value-differents.png (88.6 kB)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Searching a function to get the rotation of one Vector3 in relation to another 1 Answer

Why won't my model rotate up on X axis? 1 Answer

How to rotate a ship using the right analog stick? 0 Answers

how do I rezolve "look rotation viewing vector is zero"? 1 Answer

Rotate GameObject with children around itself on mouse drag 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