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 SonicFan · Aug 17, 2014 at 07:16 AM · rotationyaxis

Rotate object to be like target one Axis ?

I made a new scene to test rotation stuff in my game and now i have two cubes.

i want cube1 yRotation to be the same as cube2 yRotation i tried using euler angles but it's rotating in the world y axis or in the global y axis not the cube1 y axis.

Here is the script.

 #pragma strict
 
 var target : Transform;
 
 function Update () {
     transform.eulerAngles.y =  target.eulerAngles.y;
 }

I want cube1 to rotate as the target y axis in his local y axis not the world global y axis .

Thanks for any help.

Comment
Add comment · Show 6
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 robertbu · Aug 17, 2014 at 07:22 AM 1
Share

I don't thing this is generally solvable. The problem is that there are multiple ways to get to a specific rotation, and there are multiple eulerAngle respresentations for any given rotation. For example (180,0,0) is the same physical rotation (0,180,180). So in taking an arbitrary rotation, we don't know what part of the rotation with creating by rotating on the 'y'.

You can solve it for specific situations, but for that we need more information.

  • How is the target rotated (script if you have it)?

  • Since you are only rotating on the 'y', I assume the x and z axes have a rotation? If so, can you describe it?

avatar image SonicFan · Aug 17, 2014 at 09:03 AM 0
Share

1- The target will only rotate on the y axis (still don't have a script).

2- The y will be like the target and the x and z axis will be like the slopes.

avatar image Alessio89 · Aug 17, 2014 at 01:24 PM 0
Share

$$anonymous$$ay I add that you can snap an axis of an object to another axis, simply by going:

 // Local Y Axis = Target's Local Y Axis
 transform.up = Target.transform.up

But if you want more complex rotations, than I'm standing with robertbu, rotations are hells and you should be looking into Quaternions ins$$anonymous$$d of vector representation of the rotation

avatar image itsharshdeep · Aug 17, 2014 at 02:36 PM 0
Share

Hello , First of all 'm really sorry for saying this, I didnt get ur question but as much as I understand it I juat want to say that there is something : transform.rotation. eulerangles Or transform. rotation.localeulerangles

Find out something like this and all related scripts which contain this

I used this once in my game few years back

This might help you out.

Sorry If I gave u wrong answer. .

avatar image robertbu · Aug 17, 2014 at 02:41 PM 0
Share

I'm still having trouble visualizing the situation, but if x and z are modest rotations, and target will only be rotating on the 'y', then this might work:

 Vector3 v = target.forward;
 v.y = 0.0;
 transform.rotation = Quaternion.LookRotation(v);
Show more comments

1 Reply

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

Answer by Scribe · Aug 17, 2014 at 07:37 PM

The easiest way to do this is with a parent child hierachy setup. Have an empty gameobject with your cube1 as a child. On the cube1 have a script that copies the yRotation of cube2 and applies it to cube1's yRotation. then for x and z rotations, apply these to the empty parent object.

If for some reason you can't use a parent let me know and I will try and script the equivalent, I quite like the challenges that Quaternions throw up! :D

Scribe

EDIT:

 Quaternion fwd = Quaternion.FromToRotation(Vector3.forward, target.forward);
 Quaternion up = Quaternion.FromToRotation(Vector3.up, transform.up);
 transform.rotation = up*fwd;

this works 'somewhat' where it is attached to your cube1 and target is your cube2 though changing the other angles (x/z) appears to suffer some form of gimbal locking

Comment
Add comment · Show 5 · 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 SonicFan · Aug 18, 2014 at 03:58 AM 0
Share

Thanks for everyone who tried to help and thank you scribe because it worked fine,just what i wanted.

EDIT: why when i rotate the y axis 180 degrees it flips cube1.

avatar image Scribe · Aug 18, 2014 at 12:17 PM 0
Share

Hmm... I'm unable to reproduce this problem, does it always happen for you all only at specific rotations of cube1?

avatar image SonicFan · Aug 18, 2014 at 12:26 PM 0
Share

It always happen when i rotate cube2 180 degrees on the y axis.

but i think that i found the solution ins$$anonymous$$d of using

 Quaternion fwd = Quaternion.FromToRotation(Vector3.forward, target.forward);

i tried using this

  Quaternion fwd = Quaternion.FromToRotation(Vector3.right, target.right);

and it worked but i'm not sure if it's fully working.

avatar image Scribe · Aug 18, 2014 at 12:36 PM 0
Share

strange, it never appears to happen for me. I expect it is because the Quaternion returned is the shortest change, so when it goes past 180 the quaternion changes to one rotating the opposite way around.

Some possible alternatives for Quaternion fwd that you could try:

 Quaternion fwd = Quaternion.Euler(0, target.rotation.eulerAngles.y, 0);

 Quaternion fwd = Quaternion.FromToRotation(Vector3.forward, -Vector3.forward)*Quaternion.FromToRotation(-Vector3.forward, target.forward);

 Quaternion fwd = target.rotation;

Your alternative looks like it should work, though I would have expected it to give the same reflection when 'right' passes 270degrees.

I still reckon that the parenting technique might be easier with less unreliable results, but it's up to you! :)

Scribe

avatar image SonicFan · Aug 18, 2014 at 12:45 PM 0
Share

Everything is working fine now thank you :D

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

Flip over an object (smooth transition) 3 Answers

Issue with camera rotating on the Y axis 0 Answers

Rotate object with mouse 1 Answer

How to clamp Y rotation of camera? 1 Answer

Why is my Y rotation stuck at 180? 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