• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

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

Problems with rotation after zoom (camera like Sketchfab) 0 Answers

OnTriggerExit does not work 0 Answers

Rotate character 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges