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
1
Question by super powered · Jul 26, 2014 at 12:22 AM · c#rotationeulerangles

Making an objects y Rotation match another objects Z

Hi! I've been trying to figure this out for a few days now with no real progress.

I have an object that rotates on the Y axis. It has a child that billboards at the camera, and that object has a child that I need to rotate on it's Z axis.

Main>BillBoard>RotationObject

I want the Z of this RotationObject child to match the Main parent's Y, but keep it's x and y axis at 0.

But I just can't seem to get it to work. When it comes to Quaternions, I tend to get confused rather quickly. I've been trying LookAt and Euler Angles stuff, but it always gives me the wrong results.

Any help would be greatly appreciated, as I'm starting to lose a ton of time to this small feature.

Heres the Billboard script the second object is using:

 `void Update () 
 {
     Vector3 v = Camera.main.transform.position - transform.position;
     v.y = v.z = 0.0f;
     transform.LookAt(Camera.main.transform.position - v);
 }

`

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

2 Replies

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

Answer by rutter · Jul 26, 2014 at 12:47 AM

If you're thinking in terms of rotation around the X, Y, and Z axes, that refers to euler angles.

Many people find it easier to use euler angles, but they're not quite the same thing as quaternions (which Unity uses internally). Luckily, Unity provides some code hooks for going back and forth between the two:

  • Each quaternion can give its eulerAngles property

  • Given some euler angles, you can get a quaternion using the Quaternion.Euler function.

Each GameObject has a transform component, which keeps track of its position, rotation, and scale, among other things.

So, we need to do three things:

  • Grab the target's rotation, in euler angles

  • Transpose the angles to use (0,0,y)

  • Assign new rotation to our object

Something like the following JavaScript would do what you're looking for:

 var target : Transform; //set this in the inspector!
 
 function Update() {
     var euler = target.rotation.eulerAngles;   //get target's rotation
     var rot = Quaternion.Euler(0, 0, euler.y); //transpose values
     transform.rotation = rot;                  //set my rotation
 }
Comment
Add comment · Show 2 · 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 super powered · Jul 26, 2014 at 01:02 AM 0
Share

Thanks for the reply! This is a lot like one of my more successful earlier trials, but the problem I ran into, (and currently run into) with this is that even though we set the angles to (0,0,y) In the script they change to non-0 numbers in engine.

For example, the top level object is at -50 for the Euler Y.

The rotation object copies that -50 to it's Z just fine, but with this code the Y changes to 180 and -180 while the x varies between -50 and -65 for some odd reason.

         Vector3 euler = thingToCheck.rotation.eulerAngles;  
         Quaternion rot = Quaternion.Euler(0, 0, euler.y); 
         transform.rotation = rot; 

paused in engine the values of the thingToCheck object are : 0,-50,0

The values of the rotation object are: -65.18262,180,-50

(and in case it matters, the billboard object has a rotation of -65.3132,-130,0)

avatar image super powered · Jul 26, 2014 at 01:07 AM 0
Share

Figured it out. Was as simple as changing transform.rotation to transform.localRotation. Thanks a ton!

avatar image
0

Answer by LtKelleyUSMC · Nov 13, 2017 at 02:56 AM

Just thought I would share this I have an Aircraft flying along (SampleAssets from Unity), and a RadarCamera 4000 units above the Aircraft. Thanks to your example, I have used your calculations, but, in order to have the camera change, according to the Aircrafts rotation, I have had to do the following:

void Update()
{
var euler = Aircraft.transform.rotation.eulerAngles;
var rot = Quaternion.Euler( 90, 0, -(euler.y));
RadarCamera.transform.localRotation = rot;
}
The logic here is: The RadarCamera (empty GameObject), with a Camera attached to it, is set up using the normal X, Y, and Z axis'. So, then the camera is rotated down 90 degrees, so now the Z axis is pointing down, and the Y axis is pointing forward, so by rotating the Z axis in the reverse, solves my Radar issue. Thanks so much for YOUR logic.!!! (Just thought I would add...) The EDITOR in this webpage, does not make it simple to add CODE...

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Transition between rotations 0 Answers

Distribute terrain in zones 3 Answers

Limit Rotation Issues 1 Answer

Why is my transform.rotatearound reset only working on positive Y? 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