Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ROMgame · Apr 26, 2014 at 09:48 PM · rotationtransformquaternionforwardlocaleulerangles

Setting an objects angle about a local axis without localEulerAngles?

Hey guys (and gals),

I'm looking to set my objects rotation around its local z axis to a specific value (in the below case 0). The problem is that I can't work out how to do it. I know people use Mathf.Atan2 for "look at" so I thought about using it for this but can't get it working. In the below code I am attempting to set "myObject"s rotation around its local z axis to 0.

 Vector3 right = myObject.transform.right;
 float angle = (Mathf.Atan2(right.z, right.x) * Mathf.Rad2Deg) + 180;
 myObject.transform.RotateAround(myObject.transform.position, myObject.transform.forward, -angle);

So I'm trying to get its rotation around its local z axis in the second line and the adjust its rotation in the third line. The problem is that the second line just flat out doesn't work, the values it returns aren't the current rotation around the z axis and I'm not sure why.

I don't want to use eulerAngles because my objects rotation varies quite a lot meaning that sometimes rotating around the z axis is done with localEulerAngles.y and sometimes localEulerAngles.z so adjusting either one of them to 0 isn't necessarily beneficial :)

Thanks in advance,

Comment
Add comment · Show 4
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 · Apr 26, 2014 at 09:50 PM 0
Share

I don't understand your question well enough to give you an answer, but you are passing the parameters to Atan2() in the wrong order. It is Atan2(y,x) or Atan2(z,x) for almost all situations in Unity. You are doing Atan2(x,z).

avatar image ROMgame · Apr 26, 2014 at 09:57 PM 0
Share

Yeah, sorry. You're right. It was one of the things I was trying to get it working and I forgot to revert it. Thanks! (i've tried both ATan2(y,x) and ATan2(z,x) :))

As for what I'm looking for, I'm basically looking for a way to store/set the rotation about an axis without using localEulerAngles. The reason being that I want to set one of my objects rotations to the same as another objects rotation whilst preserving its local z rotation.

I figured that the easiest way of doing that would be to: 1) Store object 1s local z rotation 2) Set object 1s rotation to the same as object 2 3) Set object 1s local z rotation to its original local z rotation

avatar image robertbu · Apr 26, 2014 at 10:01 PM 0
Share

I don't think what you are attempting to do will work. If you are controlling the rotation directly through the Transform, then one solution is to use an empty game object with the visible game object as a child. Z rotations (and only Z rotation) would be done on the child, other rotations done on the parent. That way when you assigned the rotation of one object to the empty game object, the local 'z' rotation would not be effected.

avatar image ROMgame · Apr 26, 2014 at 10:39 PM 0
Share

The problem is that in order to generate that parent I'd have to set its rotation to (childRotation.x, childRotation.y, 0) and the child would then maintain its own z rotation. Setting the parents rotation in this way presents us with the same problem as before of adjusting an objects rotation (in this case the parents) to 0 around the local z axis.

Also assu$$anonymous$$g we do parent it correctly, if we do parent.eulerAngles = object2.eulerAngles then the child/object1 wouldn't have maintained its local z rotation, but rather it's local z rotation + object 2s local z rotation. Unless we can assign the parents individual axis rotations directly (I.e assign x and y, not z)?

Thanks for the help anyway :)

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

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

Related Questions

player rotation to follow the mouse 2 Answers

Instantiate GameObject towards player 0 Answers

Random.rotationUniform clarification 4 Answers

Transform a Vector by a Quaternion 1 Answer

Object not rotating when the associated trigger is triggered 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